【发布时间】:2012-11-22 04:40:19
【问题描述】:
我的 UIViewController 子类中有两个初始化函数:
- (id)init
{
self = [super init];
if (self)
{
// Custom stuff
return self;
}
return nil;
}
和
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil
bundle: nibBundleOrNil];
if (self)
{
// Custom stuff
}
return self;
}
我将 init 函数放入以避免调用 initWithNibName:bundle: 方法。我正在尝试取出 xib 文件。不幸的是,调用这个 init [[Myclass alloc] init] 通过调用 [super init] 调用 initWithNibName:bundle:。
首先,我应该在文档中的哪个位置阅读,以便我期望对父 init 方法的调用调用我自己的 initWithNibName:bundle: 方法?
其次,这对 Apple 来说是一个好的设计选择。我不明白为什么这是可取的行为? (可能是我在这里没有了解全局,所以请随时提示我。)
第三,我怎样才能最好地绕过它。我只是从我的代码中取出 initWithNibName:bundle: 吗?有没有我想要选择使用 xib 或手动实例化类的情况。
【问题讨论】:
-
你确定它正在进入你的 -(id)init 函数吗?您是否也在头文件中声明了您的 init 函数?
-
@snaker -init 来自 NSObject 本身,不需要声明。
标签: objective-c ios uiviewcontroller xib