定义了另一个init方法,但initWithNibName:bundle仍然由系统调用

时间:2013-10-12 07:33:21

标签: ios objective-c

在Same View Controller中,我想使用自定义init方法,同时禁用-initWithNibName:bundle:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSAssert(NO, @"use -initWithChannel:tag: instead");
    return nil;
}

- (instancetype)initWithChannel:(NSString *)channel tag:(NSString *)tag
{
    if (self = [super init]) {
        self.channel = channel;
        self.tag = tag;
    }
    return self;
}

但是当我致电-initWithChannel:tag:时,-initWithNibName:bundle:会在-[UIViewController init]后立即调用,为什么会这样?

1 个答案:

答案 0 :(得分:1)

当您致电[super init]初始化UIViewController来电initWithNibName时。请参阅here