What does addChildViewController actually do?

后端 未结 3 1746
执念已碎
执念已碎 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:29

    -[UIViewController addChildViewController:] only adds the passed in view controller in an array of viewControllers that a viewController (the parent) wants to keep reference of. You should actually add those viewController's views on screen yourself by adding them as a subviews of another view (e.g. the parentViewController's view). There's also a convenience object in Interface Builder to use childrenViewControllers in Storyboards.

    Previously, to keep reference of other viewControllers of which you used the views of, you had to keep manual reference of them in @properties. Having a build-in property like childViewControllers and consequently parentViewController is a convenient way to manage such interactions and build composed viewControllers like the UISplitViewController that you find on iPad apps.

    Moreover, childrenViewControllers also automatically receive all the system events that the parent receives: -viewWillAppear, -viewWillDisappear, etc. Previously you should have called this methods manually on your "childrenViewControllers".

    That's it.

提交回复
热议问题