A child view controller is removed by the following code within the child view controller's implementation:
- (void)commandFinishVC
{
[ self.view removeFromSuperview];
[ self removeFromParentViewController ];
}
The child view controller and its view are successfully removed but how is the parent notified of this? -viewDidDisappear is not called and -viewDidUnload has been deprecated.
From the examples I can find, it is always assumed that the parent view controller is the originator of the event nominating the child being removed but (to me) the child should be autonomous in it's logic for completion.
Should the remove methods be called by utilising the self.parentViewController property and using something like the following in the parent view controller?
- (void)commandFinishVC
{
[ childVC.view removeFromSuperview];
[ childVC removeFromParentViewController ];
}
Yes, your parent controller should generally control the addition and removal of the child controllers, so your second approach seems more applicable. See the Implementing a Container View Controller section of the UIViewController Class Reference.
I suspect you could get away with your former approach, where the child is removing itself, but Apple is consistent in their documentation and the WWDC 2011 session on Implementing UIViewController Containment says that the container controller bears the responsibility for managing children.
So, in answer to your question of how a child informs a parent that it's been removed, there is no established protocol for that. But this is unsurprising because it is presumed that the parent will be initiating this process. And to the extent that a child needs to initiate this process, the documentation suggests that the parent should have a public API for managing child controllers.
So while there is no protocol for a child informing the parent, there is, however, a published API by which parent controllers inform children that they have been moved/removed. Specifically, when the parent controller is removing a child, it should call willMoveToParentViewController
. This documentation explicitly says that we must perform this notification:
If you are implementing your own container view controller, it must call the
willMoveToParentViewController:
method of the child view controller before calling theremoveFromParentViewController
method...
Thus, as discussed in Adding and Removing a Child section of the View Controller Programming Guide, when removing a child, we should:
[childVC willMoveToParentViewController:nil];
[childVC.view removeFromSuperview];
[childVC removeFromParentViewController];
Unsurprisingly, the documentation for didMoveToParentViewController
is equally unambiguous: When you add a child controller, you must call didMoveToParentViewController
when the animation/transition (if any) is done. I don't know what would happen if we didn't call these two notification methods, but Apple says that we must and it therefore seems prudent to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With