1

I have a UITableView with some cells containing UITextField objects and others with UIButton objects. When one of the textFields is first responder (keyboard showing) i am having trouble resigning its first responder status. When a user clicks or touches a button in another cell, the textField is still not resigning even though i am calling [textField resignFirstResponder] on the 'did end on exit' and 'Editing did end' events. I am also calling [button becomeFirstResponder] when the button is clicked. I guess it has to do with objects being in different cells. any ideas?

EDIT:

i am resigning the first responder like this:

-(IBAction)endEditing:(id)sender {

    UITextField *textField = (UITextField *)sender;
    [textField resignFirstResponder];
}

the endEditing: method is called on both UITextField events: 'did end on exit' and 'Editing did end'.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
KDaker
  • 5,899
  • 5
  • 31
  • 44

1 Answers1

0

Where are you calling this [textField resignFirstResponder] and where is this textField declared?

Proper way would be to declare an instance variable:

UITextField *activeTextField;

And once any text field becomes active set it as activeTextField. Then when you press the button do [activeTextField resignFirstResponder]; activeTextField = nil;

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
  • check the edited question. this is how i am calling resignFirstResponder. – KDaker Aug 06 '11 at 11:52
  • i can't declare the UITextField and UIButton in the same controller because each belong to its own UITableViewCell subclass. Also the cells are generated based on an input so there can be 20 cells with text fields and 1 cell with 1 button or vice versa. – KDaker Aug 06 '11 at 11:54
  • Ah, I see. Setting a UIButton as firstResponder doesn't help, you *need* to call resignFirstResponder on the active textField for it to dismiss the keyboard. You should implement some way to tell the view controller which cell with textfield is selected and then in the other custom cell (the one with button) call resignFirstResponder on that. The most elegant solution would be notifications. If you need help implementing this post your view controller and custom cell classes. – Filip Radelic Aug 06 '11 at 12:10
  • ya i found a way to get the first responder thanks to this [question](http://stackoverflow.com/questions/1823317/how-do-i-legally-get-the-current-first-responder-on-the-screen-on-an-iphone). Once i get it i resign it on the button click. thanks! – KDaker Aug 06 '11 at 12:57