[英]Swift: Keyboard Observer via NSNotificationCenter doesn't work
[英]NSNotificationCenter observer doesn't work
我正在嘗試發布如下通知:
NSNotificationCenter .defaultCenter() .postNotificationName("name", object: nil)
從viewControllerA函數中
然后在viewControllerB中的viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething:", name:"name", object: nil)
但是doSomething:
從來沒有被調用!
有任何想法嗎?
這是您的兩個控制器中的代碼所需要的。
在訂閱/監聽ViewController中 :
func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething:", name: "name", object: nil)
}
func doSomething(notification: NSNotification) {
print("Notification was posted.")
}
在發布/發布ViewController中 :
func viewDidLoad() {
NSNotificationCenter.defaultCenter().postNotificationName("name", object: nil)
}
如果它不起作用,則可能是由於您的應用程序的體系結構:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.