3

I search a lot about it on stack overflow but according to their solution my program is same as mention but still not working.

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector:Selector(("keyboardWillShow:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
}


func keyboardWillShow(notification:NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification: notification)
}
Forge
  • 6,538
  • 6
  • 44
  • 64
Majid
  • 41
  • 9

2 Answers2

6

Your argument for selector should be #selector(keyboardWillShow), like so:

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}

func keyboardWillShow(notification:NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification: notification)
}
M-P
  • 4,909
  • 3
  • 25
  • 31
  • i wrote it as u mention but is still giving an error and showing warning on Selector argument that no method declared with objective-c "keyboardWillShow" – Majid Oct 14 '16 at 02:35
  • I've updated my answer. The new syntax for Swift is: `#selector(keyboardWillShow)` – M-P Oct 14 '16 at 02:52
  • Thanks buddy @Michael Patzer – Majid Oct 14 '16 at 04:03
1

If you don't use #selector then it will give uncaught exception of NSType and hence it will terminate the app.

Rouny
  • 284
  • 5
  • 18