Martin Q Martin Q - 2 years ago 158
Swift Question

No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification

After the recent update of Xcode, this code that used to work no longer works. Most of the Selector(":") has an auto correction with the exception for this code:

override func viewDidLoad() {

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

which flags an error:

No method declared with Objective C selector 'keyboardWillSHow:'

This image show different attempts which have all failed.

enter image description here

What is the new syntax for this code?

Answer Source

Assign the Selector as below:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);

And the method to update what you want:

func keyboardWillShow(notification: NSNotification) {

     //Update UI or Do Something


Same way you can do for UIKeyboardWillHideNotification.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download