UIViewController -viewDidLoad not being called

前端 未结 10 2177
感动是毒
感动是毒 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: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.

提交回复
热议问题