ios - 当呈现的 ViewController 被关闭时得到通知

标签 ios notifications presentviewcontroller

我正在使用 presentViewController 呈现一个 ViewController。当呈现的 ViewController 自行关闭时,我需要执行一些操作。目前,我为呈现的ViewController定义了一个协议(protocol),并在呈现的ViewController的dismissViewControllerAnimated的完成 block 中调用相应的方法。有没有更直接的方法?

最佳答案

我相信使用委托(delegate)是最好的方法。但是您仍然可以在类 NSNotificationCenter 的帮助下使用其他替代方法。

您可以为您的 VC(当前 VC 的父 View VC)注册/添加观察者通知

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(myVCDismissNotification:) 
        name:@"MyVCDismissNotification"
        object:nil];

在同一个类中定义该方法(它会在通知发布时被调用)

-(void) myVCDismissNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"MyVCDismissNotification"])
        NSLog (@"Successfully received the Dismiss notification!");
         //You can use it in your way.
}

请记住在您的 Parent VC 中使用此功能。

 [[NSNotificationCenter defaultCenter] removeObserver:self];

在现在的VC中,当你关闭VC时,调用下面的方法

 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"MyVCDismissNotification" 
        object:self];

有关通知的更多说明,请参阅 Apple Docs。快乐编码。

关于ios - 当呈现的 ViewController 被关闭时得到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236893/

相关文章:

ios - 内部带有 UITableView 的 self 调整 UITableViewCell - self 调整不起作用

ios - 如何在 ios swift 中点击时将警报中的图像显示为更大的图像

安卓 : why use a group notification?

android - 可以在不使用通知对象的情况下操作 Android 手机 LED 吗?

iOS:以编程方式呈现 View Controller

ios - 点击通知时出现弹出窗口问题(Deeplink)

ios - 发送异步请求 :queue:completionHandler: With Delegate Methods

ios - 在 iOS 上使用 Google Maps SDK 保存我的位置

ios - UserDefaults.didChangeNotification 未触发

ios - presentViewController 后导航栏不显示