Search code examples
iphoneiosuiviewuiviewcontrollerviewdidload

iPhone - self.view is nil in initWithNibName subclass, and viewDidLoad not called


I have this cascade :

Somewhere in the app

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

in 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 inherits GenericWindowController

in 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 has its File's owner set to MyCustomViewController and the view is connected.
All files are included and checked into the project.

GenericWindowController is designed to make some standard stuff.
MyCustomViewController extends this stuff to work with a custom View, as designed in MyXIB.

Why self.view is nil in GenericWindowController ?
Why viewDidLoad is not called ?


Solution

  • Incredible !!! The problem was because of a missing localization for the XIB. Added the localization, and no problem anymore.