[英]NSNotificationCenter observer doesn't work
I am trying to post a notification as follows: 我正在尝试发布如下通知:
NSNotificationCenter .defaultCenter() .postNotificationName("name", object: nil)
from within a function of viewControllerA 从viewControllerA函数中
then in ViewControllerB in viewDidLoad
然后在viewControllerB中的viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething:", name:"name", object: nil)
But the doSomething:
is never called! 但是doSomething:
从来没有被调用!
any ideas? 有任何想法吗?
Here's what the code should like in your two controllers. 这是您的两个控制器中的代码所需要的。
In the subscribing/listening ViewController : 在订阅/监听ViewController中 :
func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething:", name: "name", object: nil)
}
func doSomething(notification: NSNotification) {
print("Notification was posted.")
}
In the publishing/posting ViewController : 在发布/发布ViewController中 :
func viewDidLoad() {
NSNotificationCenter.defaultCenter().postNotificationName("name", object: nil)
}
If it's not working, it might be due to your app's architecture: 如果它不起作用,则可能是由于您的应用程序的体系结构:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.