Search code examples
iosswiftnsnotifications

how to pass an NSNotification as parameter to the selector function in Swift


I'm trying to implement an UIKeyboardWillShowNotification to handle my view position when the keyboard appear. I add my observer:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil)

Then I have the keyboardWillShow function:

func keyboardWillShow(notification: NSNotification){
    //Need to access to the notification here
}

In the function keyboardWillShow I need to receive the NSNotification to get access to the user information but I get this error:

"unrecognized selector sent to instance 0x7fd993e7d020"


Solution

  • You forgot the colon :. Change it to this:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)