I am building an app that contains a UITableViewController. In the VC, I put some text fields for accepting user input. I use a table view controller because it's easier to move the view up when the keyboard appears. See here for more info.
When I was learning how to hide the keyboard when the user taps somewhere else by watching one of thenewboston's videos, I was told to use
view.endEditing(true)
in the touchesBegan method.
I also found out how to use a swipe gesture recogniser so I could hide the keyboard when the user swipes down.
However, all of these are with normal view controllers. When I do this with a table view controller, nothing works! Both touchesBegan and UISwipeGestureRecognizer didn't work! When I swipe down or tap somewhere, the keyboard just stays there!
Why is this happening and how can I hide the keyboard in a table view controller?
EDIT:
The relevant code are all in a subclass of UITableViewController:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
}
@IBAction func swipedDown(sender: UISwipeGestureRecognizer) { // This is connected to a swipe gesture recogniser
view.endEditing(true)
}
I think that's how you normally do it, right? I tried this in a normal view controller and it worked. It just doesn't work in table view controllers.