如何让子类引用其超类的XIB?

use*_*951 3 objective-c xcode4.5

我有一个BaseClassUIViewController 类。它运作良好。BaseClass有 BaseClass.xib

然后我创建了该基类的子类。

结果发现没有连接任何插座。

我放

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    return self;
}
Run Code Online (Sandbox Code Playgroud)

是的,它就在那里。它被称为。我不确定 aDecoder 是什么。我认为它是 childClass 的 XIB 的表示,它不存在。我想用 super 的 xib 初始化 super。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

永远不会被调用。

事实上,我自己也很困惑。当我们有 xib 时,似乎就是那个 -(id)initWithCoder:(NSCoder *)aDecoder被调用的。所以我想知道 initWithNibName 是干什么用的?

我试过

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithNibName:NSStringFromClass([super class]) bundle:nil];
    return self;
}
Run Code Online (Sandbox Code Playgroud)

Fru*_*eek 5

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle是 的指定初始值设定项UIViewController。所以在你继承的类中,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:@"BaseClass" bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

每当 Apple 将一个初始化程序标记为指定初始化程序时,其他初始化程序将只指向指定初始化程序。您不需要实现其他初始化程序。

NIB 加载使用 initWithCoder,这就是为什么您会在加载笔尖时看到它被调用的原因。