从 nib 文件加载的 UIView,我可以将所有者设置为自己查看吗?

UIView that loaded from nib files , can i set the owner to the view it self?

最常用的初始化方法是

UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:@"MyRootView" owner:self options:nil] objectAtIndex:0];

在我的例子中,self 是指视图控制器,但我必须将所有者设置为视图本身,因为 .m 和 .xib 之间有很多出口,如何处理这种情况?

您应该在 view.m class 中使用初始化方法,例如:

- (id)initWithNibNamed:(NSString *)nibName{
     NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil];
     self = [nibContents lastObject];
     if(self){

        //do your code here
      }
     return self;
 }

并从 viewcontroller 调用此方法。连接视图以查看 xib。

感谢stack overflow,这种问题已经有了完美的解决方案。 https://github.com/PaulSolt/CompositeXib 关键是在自定义视图的实现中调用 loadNibNamed,而不是在创建视图的控制器中。