Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a parent view controller notified that is's child view controller has removed itself?

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 ];
}
like image 887
Roddy Avatar asked Jan 04 '13 22:01

Roddy


1 Answers

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 the removeFromParentViewController 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.

like image 135
Rob Avatar answered Sep 18 '22 18:09

Rob