iPhone 键盘返回键会将光标移动到下一个文本字段

标签 iphone keyboard return textfield text-cursor

我有一个 TableViewController,它使用分组样式并有两 (2) 个部分。第一部分有 4 行,第二部分有 3 行。我在每个单元格中放置了一个 UILabel 和一个 UITextField,并有一个自定义方法(textFieldDone:) 来在按下返回键时处理光标移动到下一个文本字段。

如果只有一个部分,那么效果很好,但我有两个:(是的,我需要两个:)

所以我开始编写一个答案,但得到的结果不起作用,我在调试过程中确实注意到单元标识符(我使用两个)只显示一个(在调试控制台中),它是仅第一个(通用单元)。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell   = nil;

    switch (indexPath.section) 
    {
        case AUTO_DETAILS:
        {
            static NSString *cellID = @"GenericCell";

            cell = [tableView dequeueReusableCellWithIdentifier:cellID];

            if (cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
                                               reuseIdentifier:cellID] autorelease];

                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 25)];
                label.tag           = kLabelTag;
                label.font          = [UIFont boldSystemFontOfSize:14];
                label.textAlignment = UITextAlignmentRight;
                [cell.contentView addSubview:label];
                [label release];


                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 200, 25)];
                textField.clearsOnBeginEditing = NO;
                [textField setDelegate:self];
                [textField addTarget:self action:@selector(topTextFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                [cell.contentView addSubview:textField];
            }

            NSInteger   row         = [indexPath row];
            UILabel     *label      = (UILabel *)[cell viewWithTag:kLabelTag];
            UITextField *textField  = nil;

            for (UIView *oneView in cell.contentView.subviews)
            {
                if ([oneView isMemberOfClass:[UITextField class]])
                    textField = (UITextField *)oneView;
            }

            label.text = [topCellLabels objectAtIndex:row];
            NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row];


            switch (row) 
            {
                case kMakeRowIndex:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.make;
                    break;
                case kModelRowIndex:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.model;
                    break;
                case kYearRowIndex:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.year;
                    break;
                case kNotesRowIndex:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.notes;
                    break;
                default:
                    break;
            }

            if (textFieldBeingEdited == textField)
            {
                textFieldBeingEdited = nil;
            }

            textField.tag = row;
            [rowAsNum release];
            break;
        }
        case AUTO_REGISTRATION:
        {

            static NSString *AutoEditCellID = @"AutoEditCellID";

            cell = [tableView dequeueReusableCellWithIdentifier:AutoEditCellID];

            if (cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
                                              reuseIdentifier:AutoEditCellID] autorelease];

                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 25)];
                label.tag           = kLabelTag;
                label.font          = [UIFont boldSystemFontOfSize:14];
                label.textAlignment = UITextAlignmentRight;
                [cell.contentView addSubview:label];
                [label release];


                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 200, 25)];
                textField.clearsOnBeginEditing = NO;
                [textField setDelegate:self];
                [textField addTarget:self action:@selector(bottomTextFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                [cell.contentView addSubview:textField];
            }


            NSInteger   row         = [indexPath row];
            UILabel     *label      = (UILabel *)[cell viewWithTag:kLabelTag];
            UITextField *textField  = nil;

            for (UIView *oneView in cell.contentView.subviews)
            {
                if ([oneView isMemberOfClass:[UITextField class]])
                    textField = (UITextField *)oneView;
            }

            label.text = [bottomCellLabels objectAtIndex:row];
            NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row];

            switch (row) 
            {
                case 0:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.vinNumber;
                    break;
                case 1:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.policyNumber;
                    break;
                case 2:
                    if ([[tempValues allKeys] containsObject:rowAsNum])
                        textField.text = [tempValues objectForKey:rowAsNum];
                    else
                        textField.text = automobile.licensePlate;
                    break;
                default:
                    break;
            }

            if (textFieldBeingEdited == textField)
            {
                textFieldBeingEdited = nil;
            }

            textField.tag = row;
            [rowAsNum release];
            break;
        }
        default:
            break;
    }
    return cell;
}

现在请记住,第一部分工作正常,该方法的代码如下:

-(IBAction)topTextFieldDone:(id)sender 
{
    UITableViewCell *cell               = (UITableViewCell *)[[sender superview] superview];
    UITableView     *table              = (UITableView *)[cell superview];
    NSIndexPath     *textFieldIndexPath = [table indexPathForCell:cell];

    NSUInteger row = [textFieldIndexPath row];
    row++;

    if (row > kNumOfEditableRows)
        row = 0;

    NSUInteger newIndex[] = {0, row};
    NSIndexPath     *newPath    = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
    UITableViewCell *nextCell   = [self.tableView cellForRowAtIndexPath:newPath];
    UITextField     *nextField = nil;

    for (UIView *oneView in nextCell.contentView.subviews) 
    {
        if ([oneView isMemberOfClass:[UITextField class]])
            nextField = (UITextField *)oneView;
    }
    [nextField becomeFirstResponder];

}

我的想法是创建第二个方法(secondSectionTextFieldDone:),如下所示

-(IBAction)bottomTextFieldDone:(id)sender 
{
    UITableViewCell *cell               = (UITableViewCell *)[[sender superview] superview];
    UITableView     *table              = (UITableView *)[cell superview];
    NSIndexPath     *textFieldIndexPath = [table indexPathForCell:cell];

    NSUInteger row = [textFieldIndexPath row];
    row++;

    if (row > 3)
        row = 0;

    NSUInteger newIndex[] = {0, row};
    NSIndexPath     *newPath    = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
    UITableViewCell *nextCell   = [self.tableView cellForRowAtIndexPath:newPath];
    UITextField     *nextField = nil;

    NSString *string = [NSString stringWithFormat:@"AutoEditCellID"];
    for (UIView *oneView in nextCell.contentView.subviews) 
    {
        NSLog(@"%@", nextCell.reuseIdentifier); /* DEBUG LOG */
        if ([oneView isMemberOfClass:[UITextField class]] && (nextCell.reuseIdentifier == string))
            nextField = (UITextField *)oneView;
    }

    [nextField becomeFirstResponder];

}

但结果并没有解决问题。

所以我的问题是,如何让光标跳转到它所在部分中的下一个文本字段,如果有,如果没有,则发送一条消息“resignFirstResponder”,以便键盘进入离开。

最佳答案

实现 UITextFieldDelegate

yourFirstTextFeld.delegate = self;
yourSecondTextFeld.delegate=self;

实现这个方法

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
 if(theTextField==yourFirstTextFeld){
     [yourSecondTextFeld becomeFirstResponder];
 }
 return YES;
}

关于iPhone 键盘返回键会将光标移动到下一个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2586467/

相关文章:

java - 方法未返回数组的正确值。我究竟做错了什么?

iphone - 如何正确转换 UIWebView 将内容更改为图像

c# - 在C#Windows窗体上处理多个按键事件

iphone - 将数据从 NSObject 发送到 UIViewController

keyboard - 阿拉伯语:如何使用 PC 键盘方便地输入 Dagger 元音(又称微型元音)和 alif-wasla

ios - 防止键盘隐藏 UITableView 底部的 UITextField

javascript - 我的 DIY trim 函数无法返回正确答案,它返回未定义

android - 使用相机快照防止多项 Activity 堆叠

iphone - 用数据填充 TableView 部分 ??

iphone - 我的应用程序在 iPhone 和 iPad 之间安装很奇怪