fabian789
fabian789

Reputation: 8412

Tell any UITextField to dismiss

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.

Upvotes: 1

Views: 1841

Answers (3)

Luke Mcneice
Luke Mcneice

Reputation: 2720

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];

Upvotes: 3

KingofBliss
KingofBliss

Reputation: 15115

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

Upvotes: 1

Philipp
Philipp

Reputation: 1963

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) {...}

Upvotes: 0

Related Questions