4

I have a UITableView containing in each cell a UITextField. When the user clicks on the UITextField, as expected, the keyboard will popup.

I have implemented in my delegate tableView: didSelectRowAtIndexPath: method to dismiss the keyboard with resignFirstResponder sent to the last UITextField used.

Everything works ok if the cell with the last UITextField used is displayed.

Now, if I scroll down to bottom of the tableview and press on a row, then the resignFirstResponder is sent to a hidden UITextField and will not hide the keyboard. It doesn't throw an error also.

How can I hide the keyboard in such cases?

Manlio
  • 10,768
  • 9
  • 50
  • 79
CristiC
  • 22,068
  • 12
  • 57
  • 89

4 Answers4

4

Have a look at UITextFieldDelegate and put the

[textField resignFirstResponder];

method in one of the callback functions. I particulary prefer:

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Teodor Kostov
  • 313
  • 1
  • 11
  • - (BOOL)textFieldShouldReturn:(UITextField *)textField; is not called at all in my case. – CristiC Jul 27 '11 at 15:29
  • This method is called when the return/done button is clicked. If this does not suit you exactly you should choose a callback for another action. – Teodor Kostov Jul 27 '11 at 20:03
  • 1
    My keyboard type is Number Pad, so I don't have return/done button. And I don't need it for anything else, so I think the method will not work... – CristiC Jul 28 '11 at 07:09
  • Have you read my last comment? My keyboard type is Number Pad. There is no return, done, go and so on... button on the bottom right side. Only delete button. – CristiC Jul 28 '11 at 14:52
  • For example there is `-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string`. This is called whenever a new character is typed. Or checkout [this post](http://stackoverflow.com/questions/1823317/how-do-i-legally-get-the-current-first-responder-on-the-screen-on-an-iphone). Or you should consider using `UIKeyboardTypeNumbersAndPunctuation`. However, if you are not able to close your keyboard through delegation, maybe you should change your code design a bit. – Teodor Kostov Jul 29 '11 at 02:59
  • 1
    Thanks. That post solved it. I just had to add [self.view endEditing:YES]; – CristiC Jul 30 '11 at 09:13
0

Did you try calling endEditing:YES method of the UITextField?

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • This should make the first responder to resign. It doesn't matter keyboard type. Not sure this will work, but you can try :) – Manlio Jul 26 '11 at 22:34
  • Nope, doesn't work. Not with endEditing or setEditing (like you initially mentioned). – CristiC Jul 26 '11 at 22:39
  • setEditing was wrong because editing property is read-only, just made a bit of confusion... – Manlio Jul 26 '11 at 22:43
0
-(IBAction)hidekey:(id) sender{

[textField resignFirstResponder];
}

if you using Interface builder then checked outlet of text field and in .m file use above function and define for that text field. It will work.

ram
  • 1,193
  • 1
  • 15
  • 27
  • what u want to ask? i give this for hid keyboard. – ram Jul 27 '11 at 07:25
  • Please explain better... What do you mean by "if you using Interface builder then checked outlet of text field"? Especially "then checked outlet of text field"? – CristiC Jul 27 '11 at 13:38
0

Please use this tutorial to Create a return Key for UIKeyboardTypeNumberPad ! This should save a lot of time for you.

Legolas
  • 12,145
  • 12
  • 79
  • 132