【问题标题】:NSNotificationCenter never called it's selector methodNSNotificationCenter 从未调用它的选择器方法
【发布时间】:2015-04-23 11:27:37
【问题描述】:

我正在向我的 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

【问题讨论】:

  • 选择器名称不正确。使用“Noti:”而不是“Noti”,因为您的函数有 1 个参数
  • 我正在尝试,但它不起作用。
  • 你也可以试试Selector("Noti:")。除了声明要使用的 Selector 之外,我没有看到您的代码存在其他问题
  • 先生,当我编写 Selector("Noti:") 时,Xcode 6.1.1 中发生错误,它会自动设置选择器:"Noti:" 所以我不能在大写单词中使用 S选择器.....当我使用选择器时:(“Noti:”)没有错误,但 SecondViewController 没有调用 addObservor 方法。
  • 不,我的意思是使用selector: Selector("Noti:") 而不是selector: "Noti:"

标签: swift nsnotificationcenter ios8.1


【解决方案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
}

【讨论】:

  • 先生,发布通知工作...但在第二个视图控制器中未收到通知..
  • 先生,没有错误......但我无法收到通知......我正在 SecondViewController 的 Viewdidload( ) 方法中编写 addObserver 代码。
  • 你能在通知名称“test:”中添加一个:并尝试
  • Nassif 先生,我在通知名称“test:”和选择器名称“Noti:”中也使用了一个:...但它没有响应.....
  • 只需将 NSNotificationCenter.defaultCenter().addObserver(self, selector: "Noti", name: "test", object: nil) 替换为 NSNotificationCenter.defaultCenter().addObserver(self, selector: " Noti:", name: "test", object: nil) 在你的 viewdidload 方法中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多