iPhone - self.view在initWithNibName子类中为nil,而未调用viewDidLoad

时间:2012-08-07 21:44:40

标签: iphone ios uiview uiviewcontroller viewdidload

我有这个级联:

应用中的某个地方

- (void) go
{
    MyCustomViewController* controller = [[MyCustomViewController alloc] init];
    controller.delegate = self;
    [controller run];
}
MyCustomViewController中的

- (id) init
{
    // there is an if statement here in real to choose another XIB if needed
    // but I only display here the code that is called in my tests
    self = [super initWithNibName:@"MyXIB" bundle:nil];
    if (!self) return nil;

    self.delegate = nil;

    return self;
}

- (void) run
{
    // do things with self.view
}

MyCustomViewController继承GenericWindowController

GenericWindowController中的

/*- (id) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return nil;

    self.view.userInteractionEnabled = NO;  // THE APP CRASHES HERE ! self.view is nil

    ...

    return self;
}*/

// METHOD created following first answers : NOT CALLED
- (void) viewDidLoad
{
    self.view.userInteractionEnabled = NO;
    // and many other things done with self.view
}

MyXIB将其文件所有者设置为MyCustomViewController,并且视图已连接 包含所有文件并将其签入项目。

GenericWindowController旨在制作一些标准内容 MyCustomViewController扩展了这些东西,以使用MyXIB中设计的自定义视图。

为什么在GenericWindowController中self.view为nil? 为什么不调用viewDidLoad

3 个答案:

答案 0 :(得分:2)

self.view仅在viewDidLoad之后有效 - 在init...中仍然是nil

答案 1 :(得分:2)

视图控制器不应尝试在initWithNibName:bundle:中访问其视图。它应该等到viewDidLoad

答案 2 :(得分:0)

难以置信!!!问题是因为缺少XIB的本地化。添加了本地化,没有问题了。