Prajoth
Prajoth

Reputation: 900

Why doesn't this UIViewController receive my NSNotification

I am having trouble getting a viewController to receive notifications from NSNotificationCenter. Where am I going wrong?

In my viewController, I have defined :

- (id)init {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveEvent:)
                                                 name:UIApplicationWillResignActiveNotification
                                               object:nil];


    return self;
}


- (void)receiveEvent:(NSNotification *)notification {
    counter = counter + 1;
    NSString *string = [NSString stringWithFormat:@"%d", counter];  
    vocabword.text = string;
}

But the text is not updating :(

Upvotes: 0

Views: 546

Answers (2)

TomSwift
TomSwift

Reputation: 39512

Is your init method being called? (and, why aren't you calling [super init] in there?)

Depending on how you construct your viewController, the init method itself may not be called. Rather, anther initializer might be used, such as initWithCoder: if being loaded from a xib.

Typically I'll register for notifications in viewDidLoad and unregister in viewDidUnload. Is there a reason you'd need to receive the notification if your view were unloaded for some reason (e.g. too much memory pressure?)

Upvotes: 1

Micah Hainline
Micah Hainline

Reputation: 14427

Nothing in the code you posted is obviously wrong. It's probably some other problem. Are you getting the log message? Are you sure your class is being created? Are you actually resigning active on the application?

Upvotes: 1

Related Questions