Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does View Controller Containment work in iOS 5?
    primarykey
    data
    text
    <p>In WWDC 2011 Session 102, Apple introduced View Controller Containment, which is the ability to create custom view controller containers, analogous to <code>UITabBarController</code>, <code>UINavigationController</code>, and the like.</p> <p>I watched the examples several times. There are a flurry of methods associated with this pattern, but it was a little hard to figure them out exactly. I'm going to post here what I think is going on and see if the community will confirm or disconfirm my suspicions.</p> <p><strong>Scenario 1: Moving from no parent to a new parent view controller</strong></p> <pre><code>[vc willMoveToParentViewController:self]; [self addChildViewController:vc]; [self.view addSubview:vc.view]; // or something like this. [vc didMoveToParentViewController:self]; </code></pre> <p>Do the first two lines have to occur in the order given, or can they be reversed?</p> <p><strong>Scenario 2: Moving from a parent view controller to no parent view controller</strong></p> <pre><code>[vc willMoveToParentViewController:nil]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; </code></pre> <p>Is it also necessary to call <code>[vc didMoveToParentViewController:nil]</code>? The examples in Session 102 <em>did not do this</em> in this scenario, but I don't know whether that was an omission or not.</p> <p><strong>Scenario 3: Moving from one parent view controller to another</strong></p> <p>This will likely occur in the following way, because the logic in each parent view controller will be encapsulated.</p> <pre><code>// In the old parent [vc willMoveToParentViewController:nil]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; // In the new parent [vc willMoveToParentViewController:self]; [self addChildViewController:vc]; [self.view addSubview:vc.view]; [vc didMoveToParentViewController:self]; </code></pre> <p><strong>Questions</strong></p> <p>My main question is this: Is this how view controller containment should work, in general? Are the mechanics given above correct?</p> <p>Is it necessary to call <code>willMoveToParentViewController</code> before calling <code>addChildViewController</code>? This seems like the logical order to me, but is it strictly necessary?</p> <p>Is it necessary to call <code>didMoveToParentViewController:nil</code> after calling <code>removeFromParentViewController</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload