2

Hihi all,

My requirement is to hide the keyboard or picker when user tap out of my UITableViewCell (but still within the UITableView).

I have found some post about overriding hitTest by subclassing UIView, but my class is subclass of UIViewController, and I need the dismissModalViewControllerAnimated method, which makes me can't change the subclass to UIView, thus not able to override the hitTest.

Is there other alternative to achieve the above requirement? Please advice. Thanks in advance.

:)

joe kirk
  • 700
  • 2
  • 10
  • 25
  • So you would be tapping on another cell right? Why not use resignFirstResponder when some other cell is tapped inside the didSelectRowAtIndexPath method? – Bourne Sep 17 '11 at 00:33
  • My UITableView is of type Grouped, would need to resignFirstResponder when tapping on area out of any cell, but within the tableView (which covers the whole screen). – joe kirk Sep 17 '11 at 00:58
  • You can also do same thing by adding UIButton on top bar named as Done, it will get appear when keyboard appears and when user clicks, then hide keyboard! – Mahesh Sep 17 '11 at 07:20
  • Thanks for the comments, have opted for AmitApollo's solution. :) – joe kirk Sep 17 '11 at 08:14

1 Answers1

2

You may be able to create an IBAction that sends the (id)sender and resignsFirstResponder add it to TouchUpOutside UITableViewCell in IB or you may be able to code it like:

 UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
 [self.tableView addGestureRecognizer:gestureRecognizer];

and add [specificfield resignFirstResponder]; to your dismissKeyboard field and make it into a void method.

That's what Dismiss keyboard by touching background of UITableView article says anyhow, and it seems similar to your problem.

Community
  • 1
  • 1
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • 1
    Thanks for the solution! Have tried the UITapGestureRecognizer method and it works great! :) – joe kirk Sep 17 '11 at 08:14
  • 1
    Might also be worth noting: If you have other tap elements on the UI, they may have trouble... A good method to overcome this: use the textField delegate to add and remove the gesture recognizer on textViewDidBeginEditing and textViewDidEndEditing – rckehoe May 10 '14 at 14:43