Is it wise to “nest” UIViewControllers inside other UIViewControllers like you would UIViews?

后端 未结 3 1662
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 00:31

I\'ve got a fairly complex view, for me anyway, that has a few \"trays\" with custom interface items on them. They slide in and out of my root view. I\'d like to nest (addSu

3条回答
  •  星月不相逢
    2020-12-05 01:12

    Prior to iOS 5.0 this will specifically not recommended because the nested view controllers' lifecycle events – viewWillAppear, etc. – won't be called. See Abusing UIViewControllers.

    With multiple UIViewController’s views visible at once some of those controllers may not receive important messages like -viewWillAppear: or -didReceiveMemoryWarning. Additionally some of their properties like parentViewController and interfaceOrientation may not be set or updated as expected.

    iOS 5.0 added containment UIViewControllers that correctly handles those lifecycle events by adding child view controllers.

    - (void)addChildViewController:(UIViewController *)childController
    

    I spent countless hours trying to get nested view controllers to work in iOS 4. I eventually did, but it required a lot of glue code that was easy to get wrong. Then I saw the warning in the docs.

提交回复
热议问题