Oliver
Oliver

Reputation: 23520

iPhone - sending a message "in the air" for any listener object

Is there a way onto the iPhone for an object to send a message without a specific receiver object, and into another object, listen to such messages, that could come with objects (parameters), and do what is needed ?

I searched around NSNotification but I don't see what I should do.

Upvotes: 0

Views: 252

Answers (3)

Rpranata
Rpranata

Reputation: 2030

I think NSNotification is the message object itself, to send to listen to what is sent try NSNotificationCenter. It has a singleton object, so to send the message:

NSNotification *notificationObj;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotification:notificationObj];

And the other class listen to with:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method:) object:nil];

Make sure that class has method: method. You can have a single parameter, which is an NSNotification object that is sent earlier. The NSNotification object has [notificationObj object which you can get as a piece of data sent by the sender class. Alternatively, you might use [notificationObj userInfo] if you want it to be more structured.

you can initialise notificationObj and tailor it with the message that you'd want. More information on NSNotificationCenter, you can find it

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/occ/cl/NSNotificationCenter

or for more information about NSNotification itself

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html

Upvotes: 0

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

Objects that want to be notified need to register to receive notifications with the notification center. Thereafter, when a notification is posted to the notification center, the notification center will check it against all the registered filters, and the corresponding action will be taken for each matching filter.

A "filter" in this case is the pair of (notification name, notification object). A nil object in the filter is equivalent to any object (the notification object is ignored in matching). The name is required.

Example:

/* Subscribe to be sent -noteThis:
 * whenever a notification named @"NotificationName" is posted to the center
 * with any (or no) object. */
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(noteThis:)
           name:@"NotificationName"
         object:nil];

/* Post a notification. */
[nc postNotificationName:@"NotificationName" object:self userInfo:someDict];

/* Handle a notification. */
- (void)noteThis:(NSNotification *)note
{
   id object = [note object];
   NSDictionary *userInfo = [note userInfo];
   /* take some action */
}

There is a more modern API using queues and blocks, but I find the old API easier to illustrate and explain.

Upvotes: 1

Moshe
Moshe

Reputation: 58087

Basically, you post a notification (NSNotification) to the shared class, NSNotificationCenter. Here's an example:

#define kNotificationCenter [NSNotificationCenter defaultCenter]
#define kNotificationToSend @"a notification name as a string"

//... Post the notification 

[kDefaultCenter postNotificationNamed:knotificationToSend withObject:nil];

Any class that wants to listen, adds itself as an observer to the notifcation center. You must remove the observer as well.

[kNotificationCenter addObserver:self selector:@selector(methodToHandleNotification) object:nil];

//... Usually in the dealloc or willDisappear method:

[kNotificationCenter removeObserver:self];

You can do more with the notification center. See the NSNotificationCenter documentation fr complete reference.

Upvotes: 1

Related Questions