Search code examples
iphoneuitableviewuitextfieldbecomefirstresponder

To move to next text fieds in table cell


In my application , there is a table view with 2 text fields per cell. Am assigning tags for each text field. On pressing done button i want to make the next text field the first responder(can be between table cells). But the code now i am using does not work . attaching the present code

    NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
    if ([firstResponder isKindOfClass:[UITextField class]]) {
        if (firstResponder.tag == nextTag ) {
            [firstResponder becomeFirstResponder];

        }

    }

}

I am not getting the text fields in the sub view array. I am using custom cell for table cells. Please help.


Solution

  • With little modification to Karim's code you can access the textVies like this:

    - (void)textViewDidEndEditing:(UITextView *)textView {
       //Get the indexPath for the currently selected cell
       NSIndexPath *cellPath = [(UITableView*)[self view] indexPathForSelectedRow];
    
       //Get the actual cell
       CustomTableCell *cell = (CustomTableCell *)[(UITableView*)[self view] cellForRowAtIndexPath:cellPath];
    
       int currentTag = textView.tag;
       int nextTag = currentTag + 1; // or something else
    
       UITextView *nextTV = [cell viewWithTag: nextTag];
       [nextTV becomesFirstResponder];
    }
    

    Hope this helps.