Adding and Removing a Child View Controller

In the simplest scenario, adding a child to a container controller takes three steps:

1. Call addChildViewController: on the parent and pass the child as the argument (for example, [self addChildViewController:childvc]).

2. Add the child controller’s view as a subview (for example, [self.view addSubview:childvc.view]).

3. Call didMoveToParentViewController: on the child with the parent as its argument (for example, [childvc didMoveToParentViewController:self]).

To remove a child view controller, the steps are almost (but not quite) mirrored:

1. Call willMoveToParentViewController: on the child, passing nil as the argument (for example, [childvc willMoveToParentViewController:nil]).

2. Remove the child ...

Get The Core iOS Developer’s Cookbook, Fifth 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.