在另一个xib中加载自定义视图有问题[重复]

时间:2021-02-27 22:25:58

This question already has an answer here:

这个问题已经有了答案:

Apologies because I see people have asked questions like this before. However, I haven't had much luck following the instructions. I'm interested in creating a custom view with a xib file and reusing it in another view controller's xib file.

对不起,因为我看到人们以前问过这样的问题。然而,我并没有很好地按照指示去做。我对使用xib文件创建自定义视图感兴趣,并在另一个视图控制器的xib文件中重用它。

Existing Posts:

现有的职位:

Using xib object inside another xib

在另一个xib中使用xib对象。

How to use a xib and a UIView subclass together?

如何同时使用xib和UIView子类?

I have already:

我已经:

  • Created a custom xib file (let's call it CustomView.xib), and corresponding .h and .m class files
  • 创建一个自定义的xib文件(我们称之为CustomView.xib),以及相应的.h和.m类文件
  • Set the File's Owner of the xib file to CustomView
  • 将xib文件的所有者设置为CustomView。
  • Created a top level UIView with other views as children (UILabel's etc)
    • NOTE this has a child UIView which is a custom view written in code as well
    • 注意,它有一个子UIView,它也是一个用代码编写的自定义视图
  • 创建了一个顶层的UIView和其他视图作为子视图(UILabel's等)注意它有一个子UIView,它也是一个用代码编写的自定义视图
  • Wired up the IBOutlets
  • 《连线》杂志的iboutlet
  • Created a ViewController.xib file
  • 创建了一个视图控制器。xib文件
  • Added a UIView, set the class to CustomView and also wired that up
  • 添加了一个UIView,将类设置为CustomView并将其连接起来

This results in a blank view showing up when I build the application.

这将导致在构建应用程序时出现空白视图。

I then tried what one of the above posts said to do which was override CustomView's initWithDecoder method and adding the following:

然后我尝试了上面一篇文章中提到的方法,即覆盖CustomView的initWithDecoder方法,并添加以下内容:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];

This unfortunately was resulting in the following error:

不幸的是,这导致了以下错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:

I thought this might be because I didn't set the top level view in CustomView.xib to be of class CustomView. So I then changed it to CustomView, and then things went into infinite recursion. Which makes sense since it's just reloading itself over and over again.

我认为这可能是因为我没有在CustomView中设置顶层视图。xib为类自定义视图。然后我把它改成了CustomView,然后事情就变成了无限递归。这是有道理的,因为它只是不断地重新加载自己。

Not sure what I missed from those previous posts, but I'd appreciate any guidance! Thanks!

我不确定我在之前的文章中漏掉了什么,但是我希望能得到一些指导。谢谢!

1 个解决方案

#1


1  

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:

Reason for the above error normally comes due the failure of the connections in the Interface Builder, that is the UIViewController object with the corresponding objects in the implementation class. Please check your all connection in the Interface Builder. Also verify the file owner in the xib file.

上述错误的原因通常是由于接口构建器中的连接失败,即实现类中具有相应对象的UIViewController对象。请检查接口构建器中的所有连接。还要验证xib文件中的文件所有者。

I hope this will solve your problem.

我希望这能解决你的问题。

#1


1  

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:

Reason for the above error normally comes due the failure of the connections in the Interface Builder, that is the UIViewController object with the corresponding objects in the implementation class. Please check your all connection in the Interface Builder. Also verify the file owner in the xib file.

上述错误的原因通常是由于接口构建器中的连接失败,即实现类中具有相应对象的UIViewController对象。请检查接口构建器中的所有连接。还要验证xib文件中的文件所有者。

I hope this will solve your problem.

我希望这能解决你的问题。