1

This has already been asked in a few different ways. In this case, I am adding a tap gesture recognizer, and I can't get it to work. I've tried a few different ways and here is where I'm at.

The error messages are:

Non-'@objc' method 'addKeyboardDiscardTapGesture()' does not satisfy requirement of '@objc' protocol 'KeyboardDiscardable'

@objc protocol KeyboardDiscardable {
    func addKeyboardDiscardTapGesture()
    func dismissKeyboard()
}

extension KeyboardDiscardable where Self: UIViewController {
    func addKeyboardDiscardTapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(Self.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
}

PS: Just to confirm, the idea is to only have to call addKeyboardDiscardTapGesture() in viewDidLoad.

Kqtr
  • 5,824
  • 3
  • 25
  • 32
  • That's interesting! I am still on Swift 3.1. Does it run? I think it pretended to compile the first time, but then the errors appeared when I ran (which sounds weird) – Kqtr Aug 25 '17 at 19:48
  • Thank you Martin. I will extend UIViewController instead. – Kqtr Aug 25 '17 at 20:07

0 Answers0