Container View Controllers

One of the new features provided by iOS 5 is the new way of combining view controllers in a custom hierarchy.

From the previous versions of the iOS SDK, I am sure you are already familiar with the view controller class.

Now, think of the navigation and tab bar controllers. You understand they act as containers of other view controllers. The split view controller is another example of container view controller.

In iOS 5, Apple provides you with a new set of methods to create your own container view controller and your hierarchy of view controllers.
The new methods are:

The first two methods of the previous list are the most important ones, because they allow you to create, modify and destroy the view controller hierarchy.

Once you designated which will be the parent view controller, you can use the -addChildViewController method to add a view controller to it. You can add as many siblings you want, and you can, obviously, add a child to an existing child. Hence, you can make things becoming very complex. But you should wonder, why that?
Well, this complexity in the structures pays you off in obtaining smaller and more reusable view controllers.

This new way of organizing view controllers is indeed really interesting, because it allows you to create very small and reusable view controllers and combine them together to build very complex functionalities. For example, imagine you are developing an iPad application with a single main view. On top of this view, you add a map view, a table view and a canvas view (a view on which you write with your finger). Now, in iOS 5, you can think of having a single view controller managing the main view (make sure to have this view controller, so that your app responds correctly to any orientation change), which contains the map view, the table view and the canvas view. Each of this view can be managed by a single view controller. In this way, you can embed the map view functionality in the map view controller, the table view functionality in the table view controller and the drawing functionality in the canvas view controller. These 3 view controllers will become the children of the main view controller and the following picture highlights the above structure:

View controller hierachy

Fig.1 Structure of the hierarchy of proposed example

Here you can see the main view (the blue one) containing the 3 subviews (the red, the yellow and the green one). Each of the view is managed by its own view controller. And these view controllers are children of the ParentViewController.

Let’s give a look at how to create the hierarchy highlighted int the previous picture. Obviously you need to create 4 view controller subclasses: ParentViewController, TableViewController, MapViewController and CanvasViewController. You can create them programmatically or using XIB files. As always, I prefer the second approach.

Once you have done that, you need to combine the view controllers. In the ParentViewController, you can load the first view controller (let’s suppose it will be the TableViewController) and you can add it to the parent using the -addChildViewController method.

So, in this example, if you already are writing in the ParentViewController class, you can add a child in this way:

The second line does the work for you.
Now, you could also remove the tableVC from its parent in this way

You can also ask the parent the list of the children view controllers:

You need to pay attention to the place where you add the each view. Yes, because after creating the view controller hierachy, you need obviously place each relative view where you like on the screen. So, you need to define the view frame and add to the desired superview.

Before building a simple example, let’s quicly give a look at the remaining methods added to the view controlle class. The -automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers by defualt returns YES. This means that the children you add to a parent automaically forward the rotation and the appearance. If you want a different behavior, you need to override this method and return NO. In this case, you are responsible for forwarding the following methods to child view controllers as appropriate:

The -willMoveToParentViewController is a called before adding a view controller to or removing it from its parent. The -didMoveToParentViewController is executed after adding a view controller to or removing it from its parent.

The -isMovingFromParentViewController returns YES to indicate that the view controller is being removed from its parent. The -isMovingToParentViewController returns YES to indicate that the view controller is being added to its parent.

The last method is used to perform a transition from a child view controller to one of its siblings.

Next time, I’ll show you how to practically use this new feature in an app.



(Visited 64 times, 1 visits today)