0

I have a custom TableView cell that contains a TextField and I want it to become the first responder as soon as the view is shown but [textcell.textfield becomeFirstResponder] does not work. I know it's because it's a custom cell in another class and I even tried it there and it didn't work. Anyone know how to pull this off?

Thanks...

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
Xcoder
  • 1,041
  • 5
  • 17
  • 29

2 Answers2

0
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = YES;
    self.title = @"Login";

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(doSetFirstResponder)
                                   userInfo:nil
                                    repeats:NO];
}

- (void) doSetFirstResponder {
    [_usernameField becomeFirstResponder];
}
0

I have a similar setup and becomeFirstResponder seems to work fine.

My custom cell:

@interface CustomCell : UITableViewCell 
{
    IBOutlet UITextField *costField;
}

And the delegate method from the controller class:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellTableIdentifer = @"CellTableIdentifer";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifer];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSUInteger row = [indexPath row];
    ReceiptItem *receiptItem = [models objectAtIndex:row];
    if (receiptItem == justAddedItem)
    {
        [cell.costField becomeFirstResponder];
        justAddedItem = nil;
    }

justAddedItem is set when the user clicks the button to add a new row to the table.

Dara Kong
  • 1,198
  • 2
  • 13
  • 11