About viewController's “viewDidLoad” and “viewWillAppear” methods

后端 未结 5 875
轻奢々
轻奢々 2020-12-09 07:02

I\'ve got a question regarding the two mentioned methods, since in my tests I don´t make clear the order they are called. I thought that, firstly, viewDidLoad i

5条回答
  •  既然无缘
    2020-12-09 07:28

    I would like to add to Caleb's answer: Don't confuse the view controller and the view! The name viewDidLoad clearly indicates that the method is invoked after the view has been loaded. It is the view controller that does the loading.

    Some pointers regarding the lifecycle of views and the order in which messages are sent:

    • Not an official Apple document, but I find this diagram really useful because it includes pretty much all of UIViewController's lifecycle overrides.
    • In the section Resource Management in View Controllers from Apple's "View Controller Programming Guide" there is a flowchart that depicts how views are initially loaded. It explains loadView and viewDidLoad, also in conjunction with storyboards.
    • The section Responding to Display-Related Notifications from Apple's "View Controller Programming Guide" explains how to respond to views appearing and disappearing (viewWillAppear: et al)
    • If you are planning on implementing a container view controller: The UIViewController class reference has a good overview of how messages need to be sent by your subclass.

    I'm stopping here. You can find more stuff yourself by googling for "uiviewcontroller life cycle".

提交回复
热议问题