NSNotification无法识别的选择器发送到Swift中的实例

时间:2014-06-18 21:12:24

标签: ios swift

我按如下方式设置了一个观察者,其中包括logYes()函数:

class SplashPageVC: UIViewController {

    func logYes() {
        println("Yes");
    }

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "logYes:", name: "userValid", object: nil)
    }
}

我已将以下IBAction连接到按钮:

class LoginVC: UIViewController {
    @IBAction func loginSubmitted(sender : AnyObject) {
        NSNotificationCenter.defaultCenter().postNotificationName("userValid", object: nil)
    }
}

点击按钮时出现以下错误:

[_TtC13Explorer12SplashPageVC self.logYes:]: unrecognized selector sent to instance 

我尝试过一堆不同的选择器,没有运气:

logYes
logYes:
logYes()
logYes():

我没有想法。任何见解? tyvm:)

参考文献:
NSNotification not being sent when postNotificationName: called
NSNotificationCenter addObserver in Swift
Delegates in swift?

1 个答案:

答案 0 :(得分:31)

我认为您的原始选择器(logYes:)是正确的 - 它是您应该重写的功能。通知观察器函数将发布的通知作为参数接收,因此您应该写:

func logYes(note: NSNotification) {
    println("Yes")
}
相关问题