How to avoid adding multiple NSNotification observer?

One way to prevent duplicate observers from being added is to explicitly call removeObserver for the target / selector before adding it again. I imagine you can add this as a category method: @interface NSNotificationCenter (UniqueNotif) – (void)addUniqueObserver:(id)observer selector:(SEL)selector name:(NSString *)name object:(id)object { [[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:object]; [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:object]; } … Read more

Text change notification for an NSTextField

If you just want to detect when the value of a text field has changed, you can use the controlTextDidChange: delegate method that NSTextField inherits from NSControl. Just connect the delegate outlet of the NSTextField in the nib file to your controller class, and implement something like this: – (void)controlTextDidChange:(NSNotification *)notification { NSTextField *textField = … Read more

Objective-C: Where to remove observer for NSNotification?

The generic answer would be “as soon as you no longer need the notifications”. This is obviously not a satisfying answer. I’d recommend, that you add a call [notificationCenter removeObserver: self] in method dealloc of those classes, which you intend to use as observers, as it is the last chance to unregister an observer cleanly. … Read more

How to pass object with NSNotificationCenter

You’ll have to use the “userInfo” variant and pass a NSDictionary object that contains the messageTotal integer: NSDictionary* userInfo = @{@”total”: @(messageTotal)}; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc postNotificationName:@”eRXReceived” object:self userInfo:userInfo]; On the receiving end you can access the userInfo dictionary as follows: -(void) receiveTestNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@”TestNotification”]) { NSDictionary* userInfo = notification.userInfo; NSNumber* … Read more