softshipper
softshipper

Reputation: 34061

tap gesture recognizer does not get fire

I want to implement an action, when if the user tap somewhere on the screen, then the keyboard should disappear.

I have an app, that look as follow
enter image description here

When I tap on textfield then the keyboard appears, that is good.
Now I want, when I tap somewhere on the screen, then the keyboard should disappear.
I implemented a tap gesture recognizer with following property value: enter image description here enter image description here

And the tap gesture recognizer I bound with following action in the view controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var numberField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func textFieldDoneEdition(sender: UITextField) {
        sender.resignFirstResponder()
    }

    @IBAction func onTapGestureRecognized(sender: AnyObject) {

        nameField.resignFirstResponder()
        numberField.resignFirstResponder()
    }

} 

Tap gesture recognizer is bound to the function. enter image description here And it does not work at all, when I tap somewhere on the screen. The keyboard is still there. What am I doing wrong?

Upvotes: 0

Views: 233

Answers (1)

jose920405
jose920405

Reputation: 8049

Your view should have the Gesture associated.

enter image description here

Your Gesture should have Sent actions

enter image description here

This should be enough to work.

If for some mystical reason, none of this works, you have an alternative for dismiss keyboard.

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)

Upvotes: 0

Related Questions