开发者

NSNotificationCenter: pass Notification between 2 classes

开发者 https://www.devze.com 2023-01-06 13:13 出处:网络
i have 2 classes. class1 gets some information through the net. when one of these informations comes, class1 has to send a notification to class2.

i have 2 classes.

class1 gets some information through the net. when one of these informations comes, class1 has to send a notification to class2.

i understood it so that i have to put

[[NSNotificationCenter defaultCenter] postNotificationName:at"anyUserNotification" object:class2];

into class1

[[NSNotificationCenter defaultCenter] addObserver:self selector:atselector(anyInteraction:) name:dontKnowTheSense object:dunno2];

have i understood the object:class2 in the postnotification right ? if yes: is it possible to make an anonymious notification, so that the sending class must not know, which and how many classes are listening ? or - at least i think so, have i understood the whole notification incorrect ?

besides the notification i dont need to p开发者_Go百科ass any data, but in one case it would be helpful to pass an enum

so, could anybody please help me ?

btw: i cant find a way to post an at on this windows-pc and i dont know, why it did not indent the code, i think i made 4 spaces


Object is not a mandatory argument you can set it to nil or the object sending the notification message.

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:notificationSenderOrNil];

When listening you can filter to only do something for notification sent by a specific sender.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sel) name:@"NotificationName" object:notificationSenderOrNil];

And you can pass your data in a dictionnary with userInfo: argument.

Is it Ok with that ?

0

精彩评论

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