5

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() {
    super.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?

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
Martin Q
  • 403
  • 1
  • 6
  • 13

5 Answers5

11

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.

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
2

Swift 3 example:

NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);

// MARK: - Actions

@objc private func keyboardWillShow(notification: Notification) {
    print("keyboardWillShow called")
}

@objc private func keyboardWillHide(notification: Notification) {
    print("keyboardWillHide called")
}
andreacipriani
  • 2,519
  • 26
  • 23
0

The swift syntax changed. Try this:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);
Andrew McKinley
  • 1,137
  • 1
  • 6
  • 11
  • It still flags an error: Type: 'SignInViewController' has no member 'KeyboardWillShow'. Unless you haven't updated you Xcode, this code will work. – Martin Q Apr 01 '16 at 21:49
  • It can't find that method. Make sure keyboardWillShow is available in that class and the names match (I noticed the error message capitalized Keyboard). It's unorthodox to start a method name with a capital letter. – Andrew McKinley Apr 01 '16 at 21:56
  • keyBoardWillShow wasn't capitalised: sorry for the typo. Still flags with the same error as mentioned above. – Martin Q Apr 01 '16 at 22:03
  • Try self.keyboardWillShow instead of the class name. – Andrew McKinley Apr 02 '16 at 01:28
  • I already tried that. Didn't work. I think this is new problem with Xcode. – Martin Q Apr 02 '16 at 05:26
0

I have had same issues and also find out that the class you refer on must also be subclassed from NSObject (which is not necc. the case in Swift) Otherwise you get the message

error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"
roger
  • 68
  • 6
0

Swift 3 syntax (just like Sohil's above):

    func someMethod(sender: Any?) {
      ...
    }

    func someBlockCallingWithSelector() {
      someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged) 
    }
Surpher
  • 517
  • 4
  • 10