anthonypliu
anthonypliu

Reputation: 12437

touchesBegan does not fire

I have the following code to hide my keyboard when the user taps the view, but touchesBegan is not firing at all:

class LoginViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var emailAddress: UITextField

    @IBOutlet var password: UITextField



    override func viewDidLoad() {

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

        //Delegate fields
        self.emailAddress.delegate = self
        self.password.delegate = self


    }

     override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        self.emailAddress.resignFirstResponder()
        self.password.resignFirstResponder()
    }

    func textFieldShouldReturn(textField: UITextField!) -> Bool{
        self.emailAddress.resignFirstResponder()
        self.password.resignFirstResponder()
        return true;
    }

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


}

This view controller is inside of a navigation controller, so not sure if it has something to do with the responder chain

Upvotes: 0

Views: 1061

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

Your code works just fine for me. (Though, typically I would use self.view.endEditing(YES) rather than resignFirstResponder on each text field.)

Most likely the view that you're tapping on is somehow preventing the event from being sent up the responder chain. It could be userInteractionEnabled, an alpha of 0, an override of touchesBegan which doesn't send the events up the responder chain, a gesture recognizer which is eating the touch events, etc. If you make a minimal test case which shows this problem, it'll probably become obvious which of these it is.

Upvotes: 1

Related Questions