我有两个类。
class1通过网络获取一些信息。当其中一个信息到来时,class1 必须向 class2 发送通知。
我明白了,所以我必须把
[[NSNotificationCenter defaultCenter] postNotificationName:at"anyUserNotification" object:class2];
进入class1
[[NSNotificationCenter defaultCenter] addObserver:self selector:atselector(anyInteraction:) name:dontKnowTheSense object:dunno2];
我理解了 postnotification 中的 object:class2 吗? 如果是:是否可以进行匿名通知,以便发送类(class)不知道哪些类(class)以及有多少类(class)正在收听? 或者 - 至少我这么认为,我对整个通知的理解是否错误?
除了通知之外,我不需要传递任何数据,但在一种情况下,传递枚举会很有帮助
那么,有人可以帮我吗?
顺便说一句:我找不到在这台 windows-pc 上发布 at 的方法,我不知道为什么它没有缩进代码,我想我做了 4 个空格
最佳答案
Object 不是强制参数,您可以将其设置为 nil 或发送通知消息的对象。
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:notificationSenderOrNil];
监听时,您可以过滤为仅对特定发件人发送的通知执行某些操作。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sel) name:@"NotificationName" object:notificationSenderOrNil];
您可以使用 userInfo: 参数在字典中传递数据。
这样可以吗?
关于iphone - NS通知中心: pass Notification between 2 classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180308/