2014-06-18 41 views
11

我已經設置了觀察者如下,其中包括logYes()功能:NSNotification無法識別的選擇發送到實例中夫特

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 

我已經嘗試了BU nch不同的選擇器,沒有運氣:

logYes 
logYes: 
logYes() 
logYes(): 

我不知道。任何見解? tyvm :)

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

回答

26

我認爲你原來的選擇(logYes:)是正確的 - 這是你的功能應該被改寫。通知觀察員函數收到張貼的通知作爲參數,所以你應該寫:

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

是的!我正在吠叫錯誤的樹。我♥你的男人。無法將此選爲另一個10分鐘的接受答案。 – kmiklas

+7

順便說一句,如果聲明的logy像「private func logYes(notification:NSNotification)」 –

+0

@ArthurGevorkyan @ArthurGevorkyan拯救了我的一天,可能會得到一個無法識別的選擇器異常!但選擇器怎麼不能私有?是不是隻在類中聲明瞭它呢? – bluenowhere

相關問題