Telling a UITextField to become first responder while embedded in a UITableViewCell Telling a UITextField to become first responder while embedded in a UITableViewCell ios ios

Telling a UITextField to become first responder while embedded in a UITableViewCell


It turned out that the text field was nil when I was trying to send it first responder. Sending becomeFirstResponder later in the life cycle worked.


I was having the same problem with a textfield I had added as a subview to a grouped table view cell - but figured out a solution after some poking around.

Assuming you have given the textfield a tag in your cellForRowAtIndexPath method, you can do something like this:

- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];    myTextField = (UITextField *)[cell viewWithTag:kMyTextFieldTag];    [myTextField becomeFirstResponder];}


Calling it in the -(void)viewDidLoad would work.

-(void)viewDidLoad{   [super viewDidLoad];   // Do any additional setup after loading the view.   [self.myTextField becomeFirstResponder]    //... any other code you need }