开发者

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 t开发者_开发百科o do this in coding in Objective-C??


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜