nsnotifications
KVO vs NSNotification vs protocol/delegates?
Use a delegate if you want to talk to only one object. For example, a tableView has a delegate – only one object should be responsible for dealing with it. Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app … Read more
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
How to stop the Observer in NSNotification to called twice?
Solution 1: The first thing is to check if the notification itself is posted twice. Solution 2: Even if the notification is posted only once, the action will be called as many times you’ve added the observer for the notification (no matter the notification is same or not). For example, the following two lines will … Read more
NSNotificationCenter vs delegation( using protocols )?
The rule of thumb here is how many clients would like to be notified of an event. If it’s mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model. If the event you emit may be … Read more
Where to remove observer for NSNotification in Swift?
As of iOS 9 (and OS X 10.11), you don’t need to remove observers yourself, if you’re not using block based observers though. The system will do it for you, since it uses zeroing-weak references for observers, where it can. And if you are using block based observers, make sure you capture self weakly using … 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 do you create custom notifications in Swift 3?
There is a cleaner (I think) way to achieve it extension Notification.Name { static let onSelectedSkin = Notification.Name(“on-selected-skin”) } And then you can use it like this NotificationCenter.default.post(name: .onSelectedSkin, object: selectedSkin)
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