什么是 NSNotification?
有人能解释一下 NSNotificationCenter 的重要性吗?
在哪里使用它们?
NSNotificationCenter 与 AppDelegate 有什么区别?
Can anybody explain the importance of NSNotificationCenter?
Where to use them?
What is the difference between NSNotificationCenter vs. AppDelegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Apple 在 Cocoa 库中提供了一个观察者模式,称为 NSNotificationCenter。
基本思想是监听器使用某种预定义的协议向广播者注册。在稍后的某个时刻,广播者被告知通知其所有侦听器,其中它在每个侦听器上调用某个函数并传递某些参数。这允许在两个不同的对象之间传递异步消息,这些对象不必相互了解,它们只需要了解广播者。
您可以在这里找到更多详细信息:http://numbergrinder.com/node/32
应用程序Delegate 是一个当 UIApplication 对象达到某些状态时接收通知的对象。在许多方面,它是一种专门的一对一观察者模式。
您可以在这里阅读更多相关信息:AppDelegate 的用途是什么?我如何知道何时使用它?
Apple has provided an Observer Pattern in the Cocoa library called the NSNotificationCenter.
The basic idea is that a listener registers with a broadcaster using some predefined protocol. At some later point, the broadcaster is told to notify all of its listeners, where it calls some function on each of its listeners and passes certain arguments along. This allows for asynchronous message passing between two different objects that don't have to know about one-another, they just have to know about the broadcaster.
You can find more details about it here: http://numbergrinder.com/node/32
The Application Delegate is an object which receives notifications when the UIApplication object reaches certain states. In many respects, it is a specialized one-to-one Observer pattern.
You can read more about it here: What is the AppDelegate for and how do I know when to use it?
如果您有 Actionscript 背景,那么我猜 NSNotification 就像向对象添加侦听器一样。
If you come from an Actionscript background then NSNotification is like adding listeners to objects I guess.
NSNotification
就像通知另一个类,如果另一个类中发生某些操作,将会发生的变化。NSNotification
is like notifying the other class about the changes that will happen if some action takes place in another class.