Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does View Controller Containment work in iOS 5?

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

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.

Scenario 1: Moving from no parent to a new parent view controller

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

Do the first two lines have to occur in the order given, or can they be reversed?

Scenario 2: Moving from a parent view controller to no parent view controller

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

Is it also necessary to call [vc didMoveToParentViewController:nil]? The examples in Session 102 did not do this in this scenario, but I don't know whether that was an omission or not.

Scenario 3: Moving from one parent view controller to another

This will likely occur in the following way, because the logic in each parent view controller 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: Is this how view controller containment should work, in general? Are the mechanics given above correct?

Is it necessary to call willMoveToParentViewController before calling addChildViewController? This seems like the logical order to me, but is it strictly necessary?

Is it necessary to call didMoveToParentViewController:nil after calling removeFromParentViewController?

like image 387
Gregory Higley Avatar asked Dec 05 '11 00:12

Gregory Higley


People also ask

What is view controllers in iOS?

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.

What is the view controller responsible for view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

What are view controllers in Xcode?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.


1 Answers

The UIViewController docs are pretty clear on when and when not to call willMove/didMove methods. Check out the "Implementing a Container View Controller" documentation.

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

Also, there is an example worked out here and sample code here.

Good Luck

like image 189
timthetoolman Avatar answered Oct 19 '22 22:10

timthetoolman