iphone - NS通知中心: pass Notification between 2 classes

标签 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];

我理解了 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/

相关文章:

ios - 如何使用自动布局将按钮放在屏幕底部

javascript - Crossrider 浏览器扩展 : Bidirectional communication between frontend and background

ios - AFNetworking 代码给我内存泄漏

iphone - 创建静态库iOS错误

iOS 应用程序唤醒时 BLE 在 iOS9.3 上无法正常工作?

xcode - 从 OSX 上的沙箱中访问温度和风扇数据?

iphone - CGContextClip空路径错误(自定义UINavigationBar背景)

Xcode 项目与 Xcode 工作区 - 差异

asp.net - 获取文本框的先前值asp.net vb.net

java - 如何在此对象中应用多态性