简体   繁体   English

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

[英]NSNotification unrecognized selector sent to instance in Swift

I've set up an observer as follows, which includes the logYes() function: 我设置了一个观察器,如下所示,其中包括logYes()函数:

class SplashPageVC: UIViewController {

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

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

I've wired the following IBAction to a button: 我已将以下IBAction连接到按钮:

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

I am getting the following error when I tap the button: 点击按钮时出现以下错误:

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

I have tried a bunch of different selectors, with no luck: 我尝试了很多不同的选择器,但是没有运气:

logYes
logYes:
logYes()
logYes():

I'm out of ideas. 我没主意了。 Any insights? 有什么见解吗? tyvm :) tyvm :)

References: 参考文献:
NSNotification not being sent when postNotificationName: called 调用postNotificationName:时未发送NSNotification
NSNotificationCenter addObserver in Swift Swift中的NSNotificationCenter addObserver
Delegates in swift? 代表们迅速?

I think your original selector ( logYes: ) is correct – it's your function that should be rewritten. 我认为您原来的选择器( logYes:是正确的–应该重写您的函数。 Notification observer functions receive the posted notification as an argument, so you should write: 通知观察器函数将发布的通知作为参数接收,因此您应该编写:

func logYes(note: NSNotification) {
    println("Yes")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM