1

Possible Duplicate:
Dismiss keyboard by touching background of UITableView

How do I make the keyboard go away when the user clicks somewhere else?

Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.

I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.

So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.

Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?

Community
  • 1
  • 1
Matthew
  • 21
  • 4
  • Do all UITableViewCells have textfields? If only one,then you can use UITouches to note if the touch is registered somewhere outside of the textfield in order to make the keyboard disappear – Vaishnavi Naidu Dec 10 '10 at 11:20
  • No some of the UITableViewCells have UIButtons some have UITextFields, there is a full range of different UITableViewCells – Matthew Dec 28 '10 at 13:19

2 Answers2

0

There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
0

You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,

-(void) hideKeyboard {
     [textFieldName resignFirstResponder];
     [textFieldSurname resignFirstResponder];
     for (UITextField * txtField in arrTextFields) {
         [txtField resignFirstResponder];
      }
}

Then, at various sections in your class, depending on the functionality required, call;

[self hideKeyBoard];

This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.

How to touch any part of the screen to make the keyboard go away To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.

Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Community
  • 1
  • 1
nspire
  • 1,567
  • 23
  • 26