如何通知父视图控制器的子视图控制器已自行删除? [英] How is a parent view controller notified that is's child view controller has removed itself?

查看:111
本文介绍了如何通知父视图控制器的子视图控制器已自行删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在子视图控制器的实现中,子视图控制器被以下代码删除:

A child view controller is removed by the following code within the child view controller's implementation:

- (void)commandFinishVC
{
    [ self.view removeFromSuperview];
    [ self removeFromParentViewController ];
}

子视图控制器及其视图已成功删除,但父级如何通知这个的? -viewDidDisappear未被调用且-viewDidUnload已被弃用。

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.

应该通过利用self.parentViewController属性调用remove方法并使用类似于父视图控制器中的以下内容?

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 ];
}


推荐答案

是的,您的父控制器应该通常控制子控制器的添加和删除,因此您的第二种方法似乎更适用。请参阅实现 UIViewController类参考的容器视图控制器部分。

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.

我怀疑你可以逃脱以前的方法,其中孩子正在移除自己,但Apple在他们的文档和WWDC 2011会议上一致的实现UIViewController遏制说容器管理员负责管理孩子。

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.

因此,在回答您关于孩子如何通知父母已被删除的问题时,没有建立的协议。但这并不令人惊讶,因为假设父母将启动这一过程。并且,如果孩子需要启动此过程,文档建议父母应该有一个公共API来管理子控制器。

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.

所以虽然没有协议但是,对于告知父母的孩子,有一个公布的API,父母控制者通过该API告知孩子他们已被移动/移除。具体来说,当父控制器删除一个孩子时,它应该调用 willMoveToParentViewController 此文档明确表示我们必须执行此通知:

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:


如果要实现自己的容器视图控制器,则必须调用 willMoveToParentViewController:调用 removeFromParentViewController 方法之前子视图控制器的方法...

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

因此,如 View Controller编程指南中的apple_ref / doc / uid / TP40007457-CH18-SW13>添加和删除子项部分,我们应该:

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];

不出所料, didMoveToParentViewController 同样明确:当您添加子控制器时,必须在动画/转换(如果有)完成时调用 didMoveToParentViewController 。我不知道如果我们不调用这两种通知方法会发生什么,但Apple说我们必须这样做,因此这样做似乎是谨慎的。

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.

这篇关于如何通知父视图控制器的子视图控制器已自行删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆