How does the restriction on viewing the controller work in iOS 5?

In the 102nd WWDC 2011 session, Apple introduced the View Controller Containment, which is the ability to create custom view controller containers, similar to UITabBarController , UINavigationController and the like.

I watched examples several times. There are a flurry of methods associated with this drawing, but it was a little difficult to figure them out accurately. I'm going to post here what I think is happening and see if the community confirms or not confirms my suspicions.

Scenario 1: moving from parent to new parent view controller

 [vc willMoveToParentViewController:self]; [self addChildViewController:vc]; [self.view addSubview:vc.view]; // or something like this. [vc didMoveToParentViewController:self]; 

Should the first two lines occur in this order, or can they be canceled?

Scenario 2: Moving from the parent view controller to the parent view controller

 [vc willMoveToParentViewController:nil]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; 

Also need to call [vc didMoveToParentViewController:nil] ? The examples in session 102 did not do this in this scenario, but I do not know if this was an omission or not.

Scenario 3: moving from one parent view controller to another

This is likely to happen as follows, since the logic in each controller of the parent view will be encapsulated.

 // 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]; 

Questions

My main question is this: should this work with the controller controller? Are the above mechanics correct?

Do I need to call willMoveToParentViewController before calling addChildViewController ? For me, this seems like a logical order, but is it strictly necessary?

Do I need to call didMoveToParentViewController:nil after calling removeFromParentViewController ?

+103
ios uiviewcontroller ios5
source share
2 answers

The UIViewController pretty clear when and when not to call the willMove / didMove . Check out "Container Controller Implementation . "

The docs say that if you do not override addChildViewController , you do not need to call the willMoveToParentViewController: method. However, after the transition is complete, you need to call the didMoveToParentViewController: method. "In addition, the container view manager answers the call to the willMoveToParentViewController: method before calling the removeFromParentViewController method. The removeFromParentViewController method calls the didMoveToParentViewController: controller of the child view."

In addition, here is an example here and sample code here .

Luck

+71
source share

This part is incorrect:

 [vc willMoveToParentViewController:self]; [self addChildViewController:vc]; [self.view addSubview:vc.view]; // or something like this. [vc didMoveToParentViewController:self]; 

According to the docs:

When your custom container calls the addChildViewController: method, it will automatically call the willMoveToParentViewController: method of the view controller, which will be added as a child before adding it.

Thus, you do not need a call to [vc willMoveToParentViewController:self] . This is done automatically when you call [self addChildViewController:vc] . Here's the code example again:

 [self addChildViewController:vc]; // [vc willMoveToParentViewController:self] called automatically [self.view addSubview:vc.view]; // or something like this. [vc didMoveToParentViewController:self]; 

To remove view controllers:

The removeFromParentViewController method automatically calls the didMoveToParentViewController: method of the child view controller after removing the child.

Presumably this call is [oldVC didMoveToParentViewController:nil] .

 [vc willMoveToParentViewController:nil]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; // [vc didMoveToParentViewController:nil] called automatically 
+21
source share

All Articles