iphone - 关闭 UIViewController 后向parentViewController发送消息

标签 iphone ios objective-c uiviewcontroller modalviewcontroller

这对你们中的一些人来说应该很简单。

我正在消除一个由另一个 UIViewController 从其内部以模态方式呈现的 UIViewController 。我正在使用

执行此操作
[self dismissViewControllerAnimated:YES
                         completion:^{
    // Want to access presentingViewController here.
}];

我尝试记录 selfnavigationControllerparentViewControllerpresentingViewController,但它们都返回

苹果的文档说:

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/

相关文章:

c# - 如何让父 UIView 出现在它的 UIImageView subview 之上

iphone - 比较两个 NSString 类型的字符串时应用程序崩溃

iphone - 当 url 字符串参数有空格时,NSURL 值变为 null ..?

iphone - 位置服务 iOS 提醒回调

ios - 用相同像素颜色覆盖的区域并更改它

iOS:点击并按住单元格后禁用 UITableView 选择

objective-c - NSData 内存未释放 - ARC

ios - UIWebView iOS10 上没有声音

ios - 使用 RxSwift 定期调用 API

objective-c - 如何实现基于 Cocoa 的 Adob​​e Photoshop 插件