UIViewController -viewDidLoad not being called

前端 未结 10 2114
感动是毒
感动是毒 2020-12-09 07:52

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

I have a UIViewController s

相关标签:
10条回答
  • 2020-12-09 08:31

    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)

    0 讨论(0)
  • 2020-12-09 08:39

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

    0 讨论(0)
  • 2020-12-09 08:41

    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.

    0 讨论(0)
  • 2020-12-09 08:41

    Apart from other answers here,

    It often happens when the identifier with which you instantiate your ViewController from the storyboard is incorrect. For e.g.

    [[self getStoryboard] instantiateViewControllerWithIdentifier:MyVC];

    If MyVC is the identifier of some other ViewController, this might happen.

    OP is using nib instead of storyboard here. But the answer applies.

    0 讨论(0)
  • 2020-12-09 08:42

    Ok, I have a partial answer - maybe the gurus can explain some more. The problem is:

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

    Looking more closely, in this case self.navigationController is nil - so the push message is going no-where.

    Instead, if I send:

    [self.view addSubview:self.myViewController.view];
    

    Then the view appears and -viewDidLoad is called.

    I'm not entirely sure why self.navigationController is not set in this instance - the only thing I can think of is that self is a subclass of UIViewController rather than UITableViewController (where the pushViewController code came from).

    Also, silently allowing messages to go to nil seems like a bad idea, although these answers say otherwise. See also my question here.

    Final edit:

    Answers in comments below, I've realised the display function that I was actually after (given myViewController is modal) is:

    [self presentModalViewController:myViewController animated:YES];
    

    Thanks everyone for their helpful responses.

    0 讨论(0)
  • 2020-12-09 08:44

    SOLUTION FOUND!!!!!

    Even something as innocuous as this makes the viewDidLoad method call happen.

    Insert this right after alloc initWithNibName

    viewController.view.hidden = NO; //calls viewDidLoad
    
    0 讨论(0)
提交回复
热议问题