1

I have the following subscription in swift. I need to know when the keyboard will show to move the view up. It compiles and works as expected but I don't know how to get rid of this warning. "No method declared with Objective-C selector'keyboardWillShow'"

// Subscribing class to recieve the notification
func subscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil)
}
// Shifting view's frame up
func keyboardWillShow(notification: NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification)
}
// Getting keyboard height
func getKeyboardHeight(notification: NSNotification) -> CGFloat {
    let userInfo = notification.userInfo
    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect
    return keyboardSize.CGRectValue().height
}

Thanks in advance for any suggestions!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please, check this answer, http://stackoverflow.com/questions/36366116/no-method-declared-with-objective-c-selector-for-notification-uikeyboardwillshow – Sanoj Kashyap Aug 31 '16 at 18:46

1 Answers1

1

Use #selector:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Thanks for your answer and pointing up that this question was already answered :) I didn't find it on google or as a duplicate while making the question. – Mateo Villagomez Aug 31 '16 at 19:10