UIViewController -viewDidLoad not being called

2019-01-18 01:49发布

Being new to Cocoa, I'm having a few issues with Interface Builder, UIViewController and friends.

I have a UIViewController subclass with a UIView defined in a xib, and with the controller's view outlet connected to the view. The xib's "file's owner" is set as myViewcontroller subclass.

In this one instance, the following code to load the controller/view (from the main view controller) doesn't work as expected:

if ( self.myViewController == nil )
{
    self.myViewController = [[MyViewController alloc]
        initWithNibName:@"MyViewController" bundle:nil];
}

[self.navigationController 
    pushViewController:self.myViewController animated:YES];

In MyViewController's methods, I have placed breakpoints and log messages to see what is going on:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSLog(@"initWithNibName\n");
    }

    return self;
}

-(void)viewDidLoad {

    [super viewDidLoad];
    NSLog(@"viewDidLoad\n");
}

Expected result

Both -initWithNibName and -viewDidLoad methods are called, and myViewController's view is displayed.

Observed result

Only -initWithNibName is called, the view is not displayed.

Have I missed something? Can anyone recommend anything to check? (Particularly in the wondrously opaque Interface Builder tool).

9条回答
Explosion°爆炸
2楼-- · 2019-01-18 02:09

Another reason, somewhat obvious in retrospect: if viewController.view is set in code, then the viewDidLoad event will not trigger.

查看更多
冷血范
3楼-- · 2019-01-18 02:15

RE: SOLUTION FOUND!!!!!

Indeed that seems to be a working solution, however the real trick is not in setting the view.hidden property to NO, what makes the view load from the nib file is the calling of the UIViewController's view method, the view only actually gets loaded from the nib when the view method is called for the first time.

In that sense, a simple [viewController view] message would force the view to load from the nib file.

查看更多
淡お忘
4楼-- · 2019-01-18 02:15

It looks like a capitalization problem to me. You're referencing the class MyViewController instead of the property myViewController in the call to pushViewController.

查看更多
\"骚年 ilove
5楼-- · 2019-01-18 02:16

Simply use

- (void)viewDidAppear:(BOOL)animated{ 
        [super viewDidAppear:animated];
        //Your Code here
  }

instead of the viewDidLoad method.

查看更多
Ridiculous、
6楼-- · 2019-01-18 02:23

make sure that the view outlet in File's Owner (your viewController subclass) is connected to the actual view (i.e. the 480X320 canvas you see on your screen that you use to build your UI)

查看更多
The star\"
7楼-- · 2019-01-18 02:27

Chances are that you might not have linked the supposed ViewController in main.storyboard from the Identity Inspector to the custom class you created. You might be able to navigate to that controller from other view controllers via segues but any of viewDidLoad(), viewWillAppear() etc. won't be executed.

查看更多
登录 后发表回答