2

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

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


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

回顾一下:如果观察到了NSNotification消息,那么如果没有观察到allocinit不要ProvViewController

4

2 回答 2

3

尝试添加一个实例变量来保存通知是否已触发的标志,在 -handleNotification: 中设置标志,然后在适当的时候检查。

- (void)handleNotification:(NSNotification*)notification {
    _notificationWasPosted = YES;
}

...

if (!_notificationWasPosted && !self.subViewControllerProv) {
    self.subViewControllerProv = [[ProvViewController alloc] init];
}
于 2013-03-08T11:49:16.130 回答
3

那么如何做这样的事情:

[[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!");
}
于 2013-03-08T11:50:14.990 回答