iOS Swift keyboard disable code is not working when I click blank area

Viewed 24

This is my code. I did everything as same as the learning video. But does not work. I dont know why?

Thanks

import UIKit

class DetailsViewController: UIViewController {

    
    @IBOutlet weak var imageLabel: UIImageView!
    @IBOutlet weak var urunAdiLabel: UITextField!
    @IBOutlet weak var urunBedeniLabel: UITextField!
    @IBOutlet weak var urunFiyatiLabel: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let gestureRecognizer = UIGestureRecognizer(target: self, action: #selector(klavyeyiKapat))
        view.addGestureRecognizer(gestureRecognizer)

    }
    
    @IBAction func kaydetButonuTiklandi(_ sender: Any) {
    }
    

    @objc func klavyeyiKapat () {
        view.endEditing(true)
    }

}

My code viewing

1 Answers

You're supposed to instantiate a subclass of UIGestureRecognizer that recognizes a specific type of gesture. You never instantiate a UIGestureRecognizer, it's a base class for specific recognizers.

In this case you most probably need UITapGestureRecognizer:

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(klavyeyiKapat))
Related