iphone - NSNotification 的目的是什么?
你能解释一下什么是 NSNotification 的目的,以及 我可以在哪些情况下使用 它?
通知是否调用所有类 在应用程序中,或者它是否调用 特定的类,通过传递 委托?
是否可以创建1 通知,并在 多个类?
Can you please explain what is the
purpose of the NSNotification, and
what are some situations I could use
it?Does a notification call all classes
in the app, or does it call a
specific class, by passing a
delegate?Is it possible to create 1
notification, and receive it on
multiple classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 NSNotification,您可以通知多个对象有关某个事件的信息,并且您可以做到这一点,而不必关心哪些对象以及其中有多少对象正在侦听该通知。
使用
NSNotification 传递通过 NSNotificationCenter 对象,该对象负责从创建通知的对象(使用 postNotification:函数系列)获取通知,并将它们发送到注册监听特定通知的对象(要接收通知对象必须在 NSNotificationCenter 中注册,使用
addObserver:
函数)通知可以被多个对象观察 - 所有这些对象都必须在 NSNotificationCenter 中注册,
Using NSNotification you can notify multiple objects about some event and you can do that not caring about which objects and how many of them are listening for that notification.
NSNotification passes through NSNotificationCenter object that is responsible for getting notifications from objects who create them (using postNotification: functions family) and sending them to the objects who are registered to listen to specific notification (to receive notification object must register in NSNotificationCenter using
addObserver:
functions)Notification can be observed by multiple objects - all of them must just register in NSNotificationCenter,
NSNotification
对象封装信息,以便可以通过NSNotificationCenter
对象将其广播到其他对象。NSNotification 对象(称为通知)包含名称、对象和可选字典。该名称是标识通知的标签。该对象是通知的发布者想要发送给该通知的观察者的任何对象(通常是发布通知的对象)。该字典存储其他相关对象(如果有)。
NSNotification
对象是不可变的对象。NSNotification
objects encapsulate information so that it can be broadcast to other objects by anNSNotificationCenter
object.An
NSNotification
object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any.NSNotification
objects are immutable objects.1:您是否阅读过 通知编程主题 ?
2:观察者应该注册一个通知
3:是的,多个对象可以观察同一个通知
1: Have you read Notification Programming Topics ?
2: Observer should be registred for a notification
3: Yes, several object can observe the same notification