49

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 textfield properly responds to being touched. It bring up the keyboard then. It's only when telling it to becomefirstresponder programmatically that the problem happens.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Jasarien
  • 58,279
  • 31
  • 157
  • 188

5 Answers5

44

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
  • 58,279
  • 31
  • 157
  • 188
  • @Jaserien Thanks.I also had the same problem,and solved with your solution. However i stil can't understand how textfield is getting null in view lifcycle,i haven't released it anywhere,is it ARC which is doing this? Also what is the best way to solve this kind of situation,should i allocate it again ? – Abhinandan Sahgal Aug 29 '12 at 09:24
  • If something has a strong reference to the textField (i.e. it's added as a subview to another view) it shouldn't be null'd in an ARC project. Check that your textField has a strong reference holding it around. – Jasarien Aug 29 '12 at 12:53
9

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];
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Jim Rhoades
  • 3,310
  • 3
  • 34
  • 50
5

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 
}
miracules
  • 686
  • 7
  • 25
1

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.

DShah
  • 9,768
  • 11
  • 71
  • 127
  • I am having this issue too of the cursor not showing up and have to set the editing property of the uitableview to YES for it to work. Did you manage to resolve your missing cursor issue? – Ronnie Liew Jul 25 '11 at 03:41
0

Is your UITableView editing property set to YES?

Art Gillespie
  • 8,747
  • 1
  • 37
  • 34
  • 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 Nov 14 '09 at 01:08
  • 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 Nov 14 '09 at 15:26