0

I'm trying to get keyboard notifications to work in my app but somehow I keep running into this error. This is my code:

override func viewWillAppear(_ animated: Bool) {

    NotificationCenter.default.addObserver(self, selector: Selector(("keyboardDidShow:")), name: .UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide:")), name: .UIKeyboardWillHide, object: nil)

}

override func viewWillDisappear(_ animated: Bool) {

    NotificationCenter.default.removeObserver(self)

}

func keyboardDidShow(notification : Notification)
{

    print("keyboard shown")
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    {

        print(keyboardSize)

    }


}

func keyboardWillHide(notification : Notification)
{



}

I honestly can't find where I'm going wrong. I keep getting the error as stated in the question statement.

Swapnil Dhanwal
  • 399
  • 2
  • 6
  • 17
  • Wonder which instance doesn't respond to "keyboardWillShow" selector and seems like something is wrong with those "did/will" because you subscribe with -didShow selector why then you have unrecognized -willShow error? – Injectios Oct 03 '16 at 13:55
  • check once if you created the notification in any other class – Anbu.Karthik Oct 03 '16 at 14:15
  • 1
    `func keyboardWillHide(_ notification` – Leo Dabus Oct 03 '16 at 14:23
  • It is missing the underscore before the first parameter – Leo Dabus Oct 03 '16 at 14:23
  • You should change also the selector syntax to `#selector(keyboardWillHide(_:))` – Leo Dabus Oct 03 '16 at 14:26
  • adding the _ fixed the crash but nothing's printing to the logs. I get this instead: 2016-10-03 19:55:41.101281 percentageFinal[1789:331651] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2016-10-03 19:55:41.109299 percentageFinal[1789:331651] [MC] Reading from public effective user settings. – Swapnil Dhanwal Oct 03 '16 at 14:27
  • http://stackoverflow.com/a/39521302/2303865 – Leo Dabus Oct 03 '16 at 14:30

0 Answers0