子类化具有xib的子类UIViewController [英] Subclassing a subclassed UIViewController that has a xib
问题描述
我需要有一些看起来非常相似但有不同行为的UIViewControllers,所以我想我会用xib创建一个通用的UIViewController子类,然后在需要的时候将它子类化,对于那些看起来相似的UIViewController。
I need to have a few UIViewControllers that look very similar yet have different behaviours, so I thought I'd make a general UIViewController subclass with a xib, then subclass it when I need to, for those different UIViewController's that look alike.
我正在尝试实现以下
UIViewController子类(已关联xib文件) - >并且能够按照我想要的次数进行子类化(没有为孩子们添加额外的xib文件)
UIViewController subclass (that has a xib file associated) -> and being able to subclass it as many times as i'd like (without additional xib files for the children)
到目前为止我做了什么:
what I've done so far :
xib文件表示具有多个UI元素的UIViewController。
xib file represents a UIViewController with multiple UI Elements.
我已将所有连接设置为文件的所有者@ xib文件。
I've set all the connections to file's owner @ xib file.
带有xib的子类包含这个@init方法:
the subclass with the xib contains this @ init method:
self = [[[NSBundle mainBundle] loadNibNamed:
[NSString stringWithFormat:@"ParentViewController"]
owner:self options:nil] objectAtIndex:0];
当我将xib中的View属性连接到文件的所有者时,我得到一个异常,说我不能
将View属性连接到父UIViewControllers和子UIViewControllers。
when I connect the View property in the xib to file's owner I get an exception saying I can't have the View property connected to both parent and child UIViewControllers.
但是当View属性只连接到与xib关联的UIViewController时,我得到一个空白的屏幕,并且该插座不可断开。
yet when the View property is only connected to the UIViewController that the xib is associated with, I get a blank screen, and that outlet isn't disconnectable.
如果我实例化父vc而不是孩子,一切正常,
如果一切都是以编程方式完成而不是使用xib,一切正常。
If I instantiate the parent vc instead of the child, everything works fine, If everything is done programatically and not with a xib, also everything works fine.
因为这个UIViewController显示了很多UI元素,我试图用xib设置它。
since this UIViewController displays A LOT of UI elements, I'm trying to set it with a xib.
我真的不明白如何让孩子ViewControllers看起来像父的xib文件,并有自己的添加和行为。
I just don't really understand how can I get the child ViewControllers to look like the parent's xib file and have their own additions and behaviours.
推荐答案
如果你只有父类的xib(但没有子类),你可以在子类init中执行此操作:
If you only have an xib for the parent class (but none of the subclasses), you can just do this in your subclass init:
- (instancetype) init {
if (self = [super initWithNibName:@"ParentViewController" bundle:nil]) {
// init stuff for subclass
}
return self;
}
这是一个示例项目:
https://github.com/annabd351/SubClassFromParentNib
这篇关于子类化具有xib的子类UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!