在 Objective-C 中通过 NSNotificationCenter 发送和接收消息?
我正在尝试通过 Objective-C 中的 NSNotificationCenter 发送和接收消息。但是,我还没有找到任何有关如何执行此操作的示例。如何通过 NSNotificationCenter 发送和接收消息?
I am attempting to send and receive messages through NSNotificationCenter
in Objective-C. However, I haven't been able to find any examples on how to do this. How do you send and receive messages through NSNotificationCenter
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
...在另一个班级的其他地方...
... somewhere else in another class ...
要扩展 dreamlax 的示例...如果您想随通知一起发送数据
在发布代码中:
在观察代码中:
To expand upon dreamlax's example... If you want to send data along with the notification
In posting code:
In observing code:
这对我有帮助:
来源:
http://www.smipple.net/snippet/Sounden/Simple%20NSNotificationCenter%20example
This one helped me:
Source:
http://www.smipple.net/snippet/Sounden/Simple%20NSNotificationCenter%20example
还可以使用块:
Apple 文档
There is also the possibility of using blocks:
Apple's documentation
如果您使用 NSNotificationCenter 来更新视图,请不要忘记通过调用
dispatch_async
从主线程发送它:if you're using NSNotificationCenter for updating your view, don't forget to send it from the main thread by calling
dispatch_async
:SWIFT 5.1 为新手选择的答案
...在另一个班级的其他地方...
SWIFT 5.1 of selected answer for newbies
... somewhere else in another class ...