Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive data along with the event in Objective-C?

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??

like image 727
Cathy Avatar asked Feb 08 '10 04:02

Cathy


1 Answers

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.

like image 196
Kendall Helmstetter Gelner Avatar answered Sep 30 '22 14:09

Kendall Helmstetter Gelner