What exactly willMoveToParentViewController: and didMoveToParentViewController: do?

ios, objective-c, pushviewcontroller, uiviewcontroller

Solution

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.

Problem

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?

Original source