0

This failure of Apple to allow user to dismiss keyboard is taking its toll...

I have managed to add code to do this on a few views but now I have a ViewController with a TextView and a couple of TableViews.

After I use a Tap Gesture Recognizer to dismiss the keyboard, the taps on the table cells don't trigger. The code is as follows.

func tap(gesture: UITapGestureRecognizer) {
    if gesture.view!.tag != 33 {
        self.view.endEditing(true)
        titleView.resignFirstResponder()
    }
}

This might be belt & braces but I was trying to see if/what might work. (Tag 33 is the actual TextView)

The table cell is triggered in the usual way and without the tap gesture code it works fine.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)....

An IBAction tap on a button seems to work but not the cell trigger.

Brian
  • 14,610
  • 7
  • 35
  • 43
RobertyBob
  • 803
  • 1
  • 8
  • 20
  • Should this be tag == 33? – sschale Mar 02 '16 at 19:15
  • think is ok - don't want the tap inside the textview to end the editing. This was added on previous views to deal with the keyboard but works fine - the resignFirstResponder fires perfectly, just that after that the taps on the table cells are somehow blocked (maybe cos they are trying to resign a textview that is already resigned?) – RobertyBob Mar 02 '16 at 21:48

2 Answers2

0

As is often the case here, the process of posting a question leads to a self-answer....

If using TableView cell taps (and I guess in general), add the following line to the gesture setup.

gestureRecognizer.cancelsTouchesInView = false

The solution in more detail can be found here. Dismiss keyboard by touching background of UITableView

Community
  • 1
  • 1
RobertyBob
  • 803
  • 1
  • 8
  • 20
0

Hey @RobertyBob adding gesture recognizer does not allow to execute did selectRowForIndex Path.
To dismiss keyboard on I would suggest you to add code to your controller file.

Note that: Conform Protocol UITextFieldDelegate and set all textfields.delegate = self.

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];
return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];

} 



Another Best way
1. Select TextField
2. Goto attribute inspector and choose keyboard type to default and Return Key to Done

Avinash Jadhav
  • 491
  • 4
  • 17