NSNotificationCenter:在两个类之间传递通知

时间:2010-07-05 14:35:00

标签: iphone xcode message nsnotifications

我有两节课。

class1通过网络获取一些信息。当其中一个信息出现时,class1必须向class2发送通知。

我理解它,所以我必须把

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

进入class1

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

我理解了对象:后续通知中的class2对吗? 如果是:是否可以发出匿名通知,以便发送类不能知道哪些类正在监听? 或者 - 至少我是这么认为的,我是否理解整个通知不正确?

除了通知我不需要传递任何数据,但在一种情况下传递枚举会很有帮助

所以,有人可以帮助我吗?

顺便说一下:我找不到在这个windows-pc上发布at的方法,我不知道,为什么它没有缩进代码,我想我做了4个空格

1 个答案:

答案 0 :(得分:3)

对象不是必须参数,您可以将其设置为nil或发送通知消息的对象。

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

收听时,您可以过滤为仅针对特定发件人发送的通知执行某些操作。

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

您可以使用userInfo:argument。

在dictionnary中传递数据

这样可以吗?