What exactly willMoveToParentViewController: and didMoveToParentViewController: do?

konrad picture konrad · Oct 16, 2012 · Viewed 17.6k times · Source

I know that starting with iOS5 and the new UIViewController containment methods, you are supposed to call these methods together with addChildViewController:, removeFromParentViewController: and the transition method. I also know the proper order of calling them in the three scenarios. What I don't know is what exactly these methods do?

If these were merely override points for subclasses of UIViewController I guess we wouldn't be required to call super when overriding. What can/will go wrong if I don't call willMoveToParentViewController: nil before removing a view controller or didMoveToParentViewController: self?

Answer

Guillaume picture Guillaume · Oct 16, 2012

In addition to what has been said, they do call some delegate methods:

addChildViewController calls [child willMoveToParentViewController:self]

and removeFromParentViewController: calls [child didMoveToParentViewController:nil]

Also, they modify the childViewControllers property, which holds an array of child view controllers.