2

I'm trying to run the following code, unless a NSNotification is sent. I can't figure out how to put the NSNotification in the if statement:

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


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

to recap: if the NSNotification message is observed then alloc and init the ProvViewController if not then don't.

2 Answers 2

3

Well what about doing something like this:

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

and then

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

And then in your code

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

Try adding an instance variable holding a flag for whether the notification has fired or not, set the flag in -handleNotification:, and then check for if when appropriate.

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

...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.