这对你们中的一些人来说应该很简单。
我正在消除一个由另一个 UIViewController
从其内部以模态方式呈现的 UIViewController
。我正在使用
[self dismissViewControllerAnimated:YES
completion:^{
// Want to access presentingViewController here.
}];
我尝试记录 self
的 navigationController
、parentViewController
和 presentingViewController
,但它们都返回 空
。
苹果的文档说:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
据此,消息被发送到presentingViewController
。我想知道如何捕获此消息。是否有委托(delegate)调用或者我如何去查找presentedViewController是否被解雇。
最佳答案
没有公共(public) API 可以告诉您这一点。您可以收听 viewWillAppear:
并找出显示的 View Controller 被关闭的方式,但这很麻烦。我将推荐我一直推荐并被认为是最佳实践的东西:永远不要从自身中解雇 View Controller !为呈现的 View Controller 创建委托(delegate)协议(protocol),将委托(delegate)设置为呈现 View Controller ,当需要从呈现中解除呈现时,调用委托(delegate)。
为什么不搁置呈现 View Controller ,并在解散后在 block 内访问它?
__weak id weakPresentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion: ^
{
id strongPresentingViewController = weakPresentingViewController;
if(strongPresentingViewController == nil) return;
//Do what you will with the presenting view controller.
}
考虑到您当前的实现,这是最不容易出错的方法。如果呈现 View Controller 在您的 block 被调用之前被释放,您将不会遇到错误的访问崩溃。在 block 内,您强烈捕获它以确保它不会在代码中间释放。
关于iphone - 关闭 UIViewController 后向parentViewController发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667558/