views:

48

answers:

1

Hi,

I created a program to send and receive events through NSNotification. Now i need to send data along with the event. Can anyone suggest me how to do this in coding in Objective-C??

+3  A: 

There are two ways - one, you can pass any one object in with a notification - look at

+ (id)notificationWithName:(NSString *)aName object:(id)anObject

The second thing is, you can also pass an optional dictionary with as many objects as you like in it, you just need to have both sides agree on the keys used to store and retrieve the objects. That call is:

+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo

You can always pass a nil for either anObject or userInfo in either call.

An example call that sends a notification directly (you don't have to construct the notification first if you do not want to):

[[NSNotifcationCenter defaultCenter] postNotificationName:@"MyNotification" object:myObjectToSend];

There's also a variant of that call with userInfo added on, just as there is for notification construction.

Kendall Helmstetter Gelner
Thanks for the reply.Can u give a example program to do it with the first method that u have specified.
Cathy
If you can't figure it out from the information given, you are likely to simply run into a problem immediately after. It isn't the most straightforward stuff, but this isn't rocket science either. Go read the NSNotificationCenter documentation first.
bbum
If you are using notifications already aren't you using notificationWithName:object: as is? That's the easiest way to send a notification... I'll add an example anyway.
Kendall Helmstetter Gelner
Thank u for the answer..Please do add the example
Cathy
It's up there, the "postNotificationName:object:" call. The first methods I outlined were if you wanted to construct notifications by hand before sending, but there's really no need to do so and I always use that last form of call.
Kendall Helmstetter Gelner