Dismissing Keyboard using UITapGestureRecognizer


#1

Page 250: Control-drag from the top gesture recognizer in the storyboard to the implementation of DetailViewController

Does it matter where I add the implementation? In my case, I added it above the declared IBOutlets at the top which is common practice but when running the app, tapping outside text fields doesn’t seem to hide the keyboard. Any ideas? Thank you in advance!

import UIKit

class DetailVC: UIViewController, UITextFieldDelegate {

@IBAction func backgroundTapped(_ sender: UITapGestureRecognizer) {
    view.endEditing(true)
}
@IBOutlet var nameField: UITextField!
@IBOutlet var serialNumberField: UITextField!
@IBOutlet var valueField: UITextField!
@IBOutlet var dateLabel: UILabel!

var item: Item!

//MARK: NUMBER FORMATTER
let numberFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 2
    formatter.maximumFractionDigits = 2
    return formatter
}()
//MARK: DATE FORMATTER
let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .medium
    formatter.timeStyle = .none
    return formatter
}()

override func viewWillAppear(_ animated: Bool){
    super.viewWillAppear(animated)
    
    nameField.text = item.name
    serialNumberField.text = item.serialNumber
    valueField.text = numberFormatter.string(from: NSNumber(value: item.valueInDollars))
    dateLabel.text = dateFormatter.string(from: item.dataCreated)
}

override func viewWillDisappear(_ annimated: Bool) {
    super.viewWillDisappear(annimated)
    
    //Clear first responder
    view.endEditing(true)
    
    // "Save" changes to the item
    item.name = nameField.text ?? ""
    item.serialNumber = serialNumberField.text
    
    if let valueText = valueField.text,
        let value = numberFormatter.number(from: valueText) {
        item.valueInDollars = value.intValue
    }else {
        item.valueInDollars = 0
    }
}
//MARK: DISMISS THE KEYBOARD BY PRESSING THE RETURN KEY
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

}


#2

To be a bit more thorough, when I tap on any of the labels(name, serial, value) or even the Date label below(which takes up the majority of UIView), the keyboard doesn’t hide.


#3

Never mind. I see where I went wrong. I dragged and dropped the Tap Gesture Recognizer directly to the scene dock instead of the View of the DetailVC. Once I made the corrections and ran the app, keyboard now hides when I tap outside the text fields. Thanks anyways!


#4

Thanks Joe, I ran into the same problem where the UITapGestureRecognizer was not working. Turns out I made the same mistake you did. I had dragged the tap gesture recognizer directly to the scene dock instead of the view. That’s a hard issue to figure out since it looks like everything is set up correctly, yet the event wasn’t firing. I wonder what the best way to troubleshoot that is.


#5

Hi Tom, I’m glad this post helped you. I can’t recall how I figured it out but I’m betting I did some searching on StackOverflow. I probably should have pasted the link here…that’s my fault :confused:. Again, I’m glad you got it working :+1: