使用return / next键将焦点设置在以下UITextField上

时间:2011-10-18 03:47:51

标签: iphone

我有几个字段,现在就说5。我想按下返回/下一个键并大声说出用户从一个跳到另一个以在每个中输入数据。我有一个UITableViewCell

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];

        UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
        FirstField.delegate = self;
        FirstField.tag = indexPath.row;
        [cell.contentView addSubview:FirstField];
        FirstField.returnKeyType = UIReturnKeyNext;
       [FirstField release];    
    }

    // Configure the cell...

    return cell;
}

每次用户按下返回/下一个键时,我都会捕获以下内容:

// Handle any actions, after the return/next/done button is pressed

- (BOOL)textFieldShouldReturn:(UITextField *)textfield 

但即使我知道我在哪个单元格/位置/字段,我如何告诉程序关注下一个?一旦掌握了信息,我知道我必须使用becomeFirstResponder。

2 个答案:

答案 0 :(得分:2)

您可以通过使用当前标记获取nextField来实现 并使该领域成为第一响应者。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
          int previouTag = textField.tag;
    if (previouTag<=numberOfRows) {
        UITextField *tempField=(UITextField *)[self.view viewWithTag:previouTag+1]; 
        [tempField becomeFirstResponder];
    }
}

答案 1 :(得分:0)

`

 -(BOOL)textFieldShouldReturn:(UITextField *)textField  
{  
    BOOL finedNext = NO;  
    int  viewIndex = [self.mainScroll.subviews indexOfObject:textField];  
    for (int i = viewIndex+1; i < self.mainScroll.subviews.count; i++) {  
        if (!finedNext) {  
            UIView *view = [self.mainScroll.subviews objectAtIndex:i];  
            if ([view isKindOfClass:[UITextField class]]) {  
                finedNext = YES;  
                [view becomeFirstResponder];  
            }  
        }  
    }  

    return YES;  
}

`