我正在使用 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/