0

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

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
anthonypliu
  • 12,179
  • 28
  • 92
  • 154

1 Answers1

1

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.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • hmm, i checked userinteractionenabled, alpha is 1, no other overrides in my code, no gesture recognizer either. How can I make a test case for this? – anthonypliu Jul 20 '14 at 23:04
  • I get something in the console as well: "Unknown class LoginViewController in Interface Builder file." – anthonypliu Jul 20 '14 at 23:05
  • @anthonypliu That is almost certainly the problem. I would suggest searching for that issue. For example: http://stackoverflow.com/questions/24020610/how-to-access-both-objective-c-and-swift-classes-from-same-storyboard – Jesse Rusak Jul 20 '14 at 23:06