Search code examples
iosobjective-ckeyboarduitextfield

Unable to find textfield's next responder


I am trying to cycle/navigate through the UITextFields which I added as subviews to the UITableViewCells. However I am unable to get my nextResponder value in the textFieldShouldReturn: method. Can anyone advise me where my code went wrong?

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

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    }

    if (indexPath.row == 0) // first name
    {
        cell.textLabel.text = @"First Name:";
        UITextField *tempFirstNameField = [[UITextField alloc]initWithFrame:CGRectMake(100, (44-18)/2, 320-100, 32)];
        self.firstNameField = tempFirstNameField;
        self.firstNameField.font = [UIFont systemFontOfSize:14];
        self.firstNameField.tag = 1;
        self.firstNameField.returnKeyType = UIReturnKeyNext;
        self.firstNameField.delegate = self;
        [tempFirstNameField release];
        [cell.contentView addSubview:self.firstNameField];
    }
    else if (indexPath.row == 1) //last name
    {
        cell.textLabel.text = @"Last Name:";
        UITextField *tempLastNameField = [[UITextField alloc]initWithFrame:CGRectMake(100, (44-18)/2, 320-100, 32)];
        self.lastNameField = tempLastNameField;
        self.lastNameField.font = [UIFont systemFontOfSize:14];
        self.lastNameField.tag = 2;
        self.lastNameField.returnKeyType = UIReturnKeyNext;
        self.lastNameField.delegate = self;
        [tempLastNameField release];
        [cell.contentView addSubview:self.lastNameField];
    }

    return cell;
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    NSInteger nextTag = textField.tag + 1;
    NSLog(@"next tag %i",nextTag);
    // Try to find next responder

    UIResponder* nextResponder = [textField.superview.superview viewWithTag:nextTag];
    //This always returns me null value
    NSLog(@"next responder %@", nextResponder);
    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}

Solution

  • Why do you need a tableView for that, your fields seem to be static. Use a simple scrollview if the content is larger than you screen. To loop your fields you can :

    1/ use a container view for all controls you want in your navigation loop and simply loop in your subviews NSArray

    2/ best choice. Use the NSUInteger tag field to set the order in which the controls should get the focus. Begin at non-zero value because 0 is the default tag value. 10,11,12,13 and use viewWithTag: on your container view to retrieve the next control.