Tell any UITextField to dismiss

1.8k views Asked by At

How can I tell the current firstResponder to resign? I have a UITableView with a bunch of TextFields and I don't know which is active at all times. I thought about storing pointers to all cells in an array and iterating through it, telling every cell to resignFirstResponder but I'm sure there is an easier way. Maybe something like [CurrentFirstResponder resignFirstResponder]?

I would appreciate some help, Fabian

EDIT: I don't want to dismiss the keyboard when the user taps done. It should be dismissed programmatically. Since I don't know which UITextField is active at any time, I am searching for something that calls resignFirstResponder on the current FirstResponder.

3

There are 3 answers

1
Luke Mcneice On BEST ANSWER

You could keep a reference to the UITextfeild that's actively editing using textFieldDidBeginEditing: on the UITextFieldDelegate Protocol or you could do this with your parent view:

UIView * myParentViewView;//view containing one or more editable UI controls 
[myParentViewView endEditing:YES];
0
KingofBliss On

I hope this will solve your problem,

Assign delegate to UItextField,

textField.delegate=self;

then in following method

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
//This for to resign on begin editing
    [textField resignFirstResponder];
    }

 - (void)textFieldDidEndEditing:(UITextField *)textField
    {
//This for to resign on end editing
    [textField resignFirstResponder];
    }

If you dont want to the textField to be editable then,

textField.editing=NO;

Set tag to distingush your textFields

0
Philipp On

Simply use the UITextFieldDelegate (reference). Whenever - (BOOL)textFieldShouldReturn:(UITextField *)textField is called, perform [textField resignFirstResponder], since this method is always invoked with the currently active textfield. If you still need to distinguish between your textfields, try setting a tag and use it with if(textfield.tag == self.mytextfield.tag) {...}