NSNotificationCenter从未调用它的选择器方法

时间:2015-04-23 11:27:37

标签: swift nsnotificationcenter ios8.1

我正在向SecondViewController发布通知。它看起来像:

 @IBAction func NotifyButton(sender:AnyObject)
{
    NSNotificationCenter.defaultCenter().postNotificationName("test", object: nil)

}

在我的SecondViewController中,我观察到此通知。

   override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "Noti", name: "test", object: nil)

}

我的选择方法在这里:

 func Noti(notification:NSNotification)
    {
     println( "Ohhhh Notification aaya ...are wa")
}


but control is never reach to selector method. Thanks

1 个答案:

答案 0 :(得分:-1)

已发送(发布)通知

 NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

接收(获取)通知

NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)

删除通知

NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)

收到通知的方法

 func methodOfReceivedNotification(notification: NSNotification){
//Action take on Notification
}