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 🙁
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?)