ios - 如何在 if 语句中检查 NSNotification

标签 ios objective-c nsnotificationcenter

我正在尝试运行以下代码,除非发送了 NSNotification。我不知道如何将 NSNotification 放在 if 语句中:

if (!self.subViewControllerProv) {
    self.subViewControllerProv = [[ProvViewController alloc] init];
}


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

回顾一下:如果观察到 NSNotification 消息,则 allocinit ProvViewController 如果没有,则不要't.

最佳答案

那么做这样的事情怎么样:

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

然后

-(void) handleNotification {
    //BOOL _hasBeenSent in your interface
    _hasBeenSent = YES;
}

然后在你的代码中

if(_hasBeenSent) {
    NSLog(@"The notification has been sent!");
}

关于ios - 如何在 if 语句中检查 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293261/

相关文章:

objective-c - 如何从 subview 中添加 rightbarbuttonitem

ios - 通知中心在模拟器上导致 EXC_BAD_ACCESS 的原因是什么,但在设备上却没有?

ios - UIImagePickerController - 自定义覆盖上的重新拍摄按钮(Swift)

ios - 如何阻止背景阻止 Swift 4 中的其他元素?

ios - 安装Xcode 5.1后报错: Two views in the same hierarchy have the same restoration identifier

ios - Swift Objective-C 运行时类命名

ios - 推送通知到达时间

ios - 在 Realm 中存储来自 API 的数据

ios - 如何从iPhone专辑复制视频而不进行压缩?

ios - 如果我使用 ARC,-didReceiveMemoryWarning 是否还有用?