Container View Controllers

Built-in view controller subclasses such as UITabBarController, UINavigationController, and UIPageViewController are parent view controllers: they accept and maintain child view controllers and manage swapping their views into and out of the interface. Such abilities and behaviors are formalized and generalized into the notion of a container view controller, so that your own custom UIViewController subclasses can do the same thing.

A UIViewController has a childViewControllers array, which is maintained for you. To act as a parent view controller, your UIViewController subclass must fulfill certain responsibilities.

When a view controller is to become your view controller’s child, your view controller must do these things, in this order:

  • Send addChildViewController: to itself, with the child as argument. The child is automatically added to your childViewControllers array and is retained.
  • Get the child view controller’s view into the interface (as a subview of your view controller’s view), if that’s what adding a child view controller means.
  • Send didMoveToParentViewController: to the child with your view controller as its argument.

When a view controller is to cease being your view controller’s child, your view controller must do these things, in this order:

  • Send willMoveToParentViewController: to the child with a nil argument.
  • Remove the child view controller’s view from your interface.
  • Send removeFromParentViewController to the child. The child is automatically ...

Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.