What does addChildViewController actually do?

后端 未结 3 1747
执念已碎
执念已碎 2020-11-28 01:39

I\'m just dipping my feet for the first time into iOS development, and one of the first things I\'ve had to do is implement a custom container view controller - lets call it

3条回答
  •  心在旅途
    2020-11-28 02:26

    I was wondering about this question too. I watched Session 102 of the WWDC 2011 videos and Mr. View Controller, Bruce D. Nilo, said this:

    viewWillAppear:, viewDidAppear:, etc have nothing to do with addChildViewController:. All that addChildViewController: does is to say "This view controller is a child of that one" and it has nothing to do with view appearance. When they get called is associated with when views move in and out of the window hierarchy.

    So it seems that the call to addChildViewController: does very little. The side effects of the call are the important part. They come from the parentViewController and childViewControllers relationships. Here are some of the side effects that I know:

    • Forwarding appearance methods to child view controllers
    • Forwarding rotation methods
    • (Possibly) forwarding memory warnings
    • Avoiding inconsistent VC hierarchies, especially in transitionFromViewController:toViewController:… where both VCs need to have the same parent
    • Allowing custom container view controllers to take part in State Preservation and Restoration
    • Taking part in the responder chain
    • Hooking up the navigationController, tabBarController, etc properties

提交回复
热议问题