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.