It’s easy to forget which order to send the messages when adding a child view controller for containment in another parent view controller. Here’s a straight to the point look-up when you need it.
Adding a child view controller
parent.addChildViewController(child) parent.view.addSubview(child.view) child.didMoveToParentViewController(parent)
Removing a child view controller
child.willMoveToParentViewController(nil) child.view.removeFromSuperview() child.removeFromParentViewController()
Extending UIViewController for convenience
With this extension, adding a child is simplified to parent.addContainedChildViewController(child). Then when removing the child, all that needs to be done is child.breakContainment().