Part of the Cocoa Paradigms node cluster.


For API documentation relating to NSNotificationCenter see
http://developer.apple.com/techpubs/macosx/Cocoa/Reference/Foundation/ ObjC_classic/Classes/NSNotificationCenter.html


The Notification Center is a way for objects which do not know about each other to communicate.

Any Cocoa object can create and use a Notification Center but the quickest way for most coders to use this structure will be to use the default center object which can be accessed through the message [NSNotificationCenter defaultCenter]. This provides access to a system wide notification center. The exception to this case is the NSWorkspace object which has its own notification queue.

A notification is comprised of at least an arbitrary text string and also possibly a NSDictionary of information supplied by the notifying object.

An object can post a notification to a center (usually the default) and any object which is registered to observe that notification will receive it.
For example the object vineRoadTrafficLight might post the notification "iveGoneGreen" and any carInstance object which is registered to observe this will have an arbitrary method or "selector" invoked upon it, for example [carInstance localTrafficLightHasGoneGreen:notification] by the notification center upon receiving the notification.

Objects may register and deregister themselves as observers at any point in runtime with the notification center. The observers can listen for...

A specific notification from any object.
A specific notification from a specific object.
Any notification from a specific object.

Codewise it looks like this. An object will add itself

[[NSNotificationCenter defaultCenter] 
	addObserver:(id)observing_object 
           
	   selector:@selector(methodInvokedUponObserver:) 
	//note the notification is passed to the object as the argument
               
	       name:@"aHappyNotification"
         
	     object:nil];
	//object to listen upon, if nil the observer hears all notifications with the prescribed name.

And when an object which could be the same as the observer posts a notification like this

[[NSNotificationCenter defaultCenter] postNotification:
        [NSNotification notificationWithName:@"aHappyNotification"

                                      object:self]];
	//this is the object which is posting the notification

Or like this if there is additional infomation that the posting object wishes to impart

[[NSNotificationCenter defaultCenter] postNotification:
        [NSNotification notificationWithName:@"aHappyNotification"
					                                 object:self

				                                userInfo:a_Dictionary]];
	//this is an arbitrary NSDictionary of information

The notification center will invoke upon the observer the method specified like this
[observing_object methodInvokedUponObserver:postedNotification]

Effectively the notification center is a Public Address system for the run loop which allows objects to communicate in a one way fashion. All interested objects can make themselves hear about an event that has occurred.

Log in or register to write something here or to contact authors.