subhash Amale
subhash Amale

Reputation: 325

NSNotification listening

I have multiple instances of NSWindowController, who is registering for a notification to listen whenever edit happens.

Now when I edit something from one instance of windowcontroller, the notification gets posted and all the instances of that NSWindowcontroller listen to that notification,but I want only the instance which has updated its details to listen.

How can I achieve that?

Upvotes: 1

Views: 802

Answers (1)

arturdev
arturdev

Reputation: 11039

As mentioned in the documentation for [NSNotificationCenter addObserver:selector:name:object:] method for parameter object:

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

So just pass self as object.
E.G.

Registering for notifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(someSelector:)
                                                 name:@"SomeNotification"
                                               object:self]; // <- SELF!!

Posting notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"SomeNotification"
                                                            object:self //<- SELF!!
                                                          userInfo:nil];

Upvotes: 2

Related Questions