如果我有一个将 NSNotification 作为参数的函数,我是否需要添加观察者
Do I need to add observer if I have a func to take NSNotification as parameter
就像问题标题一样
假设我有这样的代码
func receieveNotification(notification : NSNotification) {
......verify notification
......retrieve userInfo
}
我还需要将观察者添加到 NSNotificationCenter.defaultCenter() 吗?
如果我做。怎么做?
是的,这是必需的。
像这样使用:摘自精彩教程的片段:
http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/
class FirstViewController: UIViewController {
@IBOutlet weak var sentNotificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNotificationSentLabel", name: mySpecialNotificationKey, object: nil)
}
// 2. Post notification using "special notification key"
@IBAction func notify() {
NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: self)
}
func updateNotificationSentLabel() {
self.sentNotificationLabel.text = "Notification sent!"
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
进一步的主题:Swift 和删除 Observers
:
http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/
当某个对象调用 NSNotificationCenter
上的 post
方法时发送 NSNotification
。然后通知中心在每个已经注册的对象上调用指定的接收方法。
如果您没有在通知中心注册,系统就无法知道它应该向您发送通知。尽管可以有其他注册中心,但在 iOS 中,您几乎总是使用默认设置。
注册通知时,指定接收通知的对象、调用该对象的方法、注册的通知以及发送者你想从中接收通知。如果你想接收每一个特定类型的通知(也就是说,你不关心哪个对象发送它),你可以指定 nil
为发件人。
因此,要注册通知,"MyNotification",并且您不关心发送它的对象是什么,您可以调用以下内容:
NSNotificationCenter.defaultCenter().addObserver(self, "gestureHandler", "MyNotification", nil)
在何处放置此调用取决于您希望此对象何时侦听它。例如,如果接收器是 UIView
,您可能希望在视图即将显示时注册,而不是在创建视图时注册。
当您想停止接收通知时,例如当接收者超出范围时,取消注册是非常重要的。您可以通过调用“removeObserver()”来完成此操作。
您应该搜索 Xcode 的文档并阅读 Notification Programming Topics。
就像问题标题一样
假设我有这样的代码
func receieveNotification(notification : NSNotification) {
......verify notification
......retrieve userInfo
}
我还需要将观察者添加到 NSNotificationCenter.defaultCenter() 吗? 如果我做。怎么做?
是的,这是必需的。
像这样使用:摘自精彩教程的片段: http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/
class FirstViewController: UIViewController {
@IBOutlet weak var sentNotificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNotificationSentLabel", name: mySpecialNotificationKey, object: nil)
}
// 2. Post notification using "special notification key"
@IBAction func notify() {
NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: self)
}
func updateNotificationSentLabel() {
self.sentNotificationLabel.text = "Notification sent!"
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
进一步的主题:Swift 和删除 Observers
:
http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/
当某个对象调用 NSNotificationCenter
上的 post
方法时发送 NSNotification
。然后通知中心在每个已经注册的对象上调用指定的接收方法。
如果您没有在通知中心注册,系统就无法知道它应该向您发送通知。尽管可以有其他注册中心,但在 iOS 中,您几乎总是使用默认设置。
注册通知时,指定接收通知的对象、调用该对象的方法、注册的通知以及发送者你想从中接收通知。如果你想接收每一个特定类型的通知(也就是说,你不关心哪个对象发送它),你可以指定 nil
为发件人。
因此,要注册通知,"MyNotification",并且您不关心发送它的对象是什么,您可以调用以下内容:
NSNotificationCenter.defaultCenter().addObserver(self, "gestureHandler", "MyNotification", nil)
在何处放置此调用取决于您希望此对象何时侦听它。例如,如果接收器是 UIView
,您可能希望在视图即将显示时注册,而不是在创建视图时注册。
当您想停止接收通知时,例如当接收者超出范围时,取消注册是非常重要的。您可以通过调用“removeObserver()”来完成此操作。
您应该搜索 Xcode 的文档并阅读 Notification Programming Topics。