views:

2143

answers:

4

I have a UITextField that is a subview of a UITableViewCell.

When my view loads, I want the text field to become first responder. I have a pointer to the text field in the table cell, so to do this I am trying:

[myTextField becomeFirstResponder];

This returns NO all the time, regardless of when it's called, which according to the docs means the text field refused to become first responder... What am I doing wrong?

Edit:

The text field properly responds to being touched. It bring up the keyboard then. It's only when telling it to become first responder programmatically that the problem happens.

A: 

Is your UITableView editing property set to YES?

Art Gillespie
No. The UITextField is a subview that I added manually, not part of the cell by default. I was unaware that the tableview needed to be in editing mode?Wouldn't this show the delete button?
Jasarien
My mistake, I read the question too quickly first time around. I just tried what you're doing and it works here. At what points in the View's life cycle have you tried sending `becomeFirstResponder` to the text field?
Art Gillespie
A: 

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];
}
Jawboxer
+1  A: 

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.

Jasarien
A: 

I was not getting the cursor to show up when the UITextField was made first responder inside of the cellForRowAtIndexPath. The text field would work just fine and dandy, but it was not showing the blinking cursor inside of the field. When you would press and hold, the zoom magnify would also not show a cursor.

I got around this odd issue by setting my becomeFirstResponder inside of the didSelectRowAtIndex method.

Justin Kramer