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 yourchildViewControllersarray 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
removeFromParentViewControllerto the child. The child is automatically ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access