【问题标题】:How to activate next and previos UITextField in a UITableView?如何在 UITableView 中激活下一个和上一个 UITextField?
【发布时间】:2013-04-10 06:25:19
【问题描述】:

我在我的应用程序中使用this one 将 UITextFields 添加到我的 UITableViewCells,我现在不知如何让用户通过使用键盘工具栏中的下一个按钮跳转到下一个项目,这里是我的代码

 IBOutlet UITextField *contNo,*breed,*coat,*color,*birthdate;
 IBOutlet UIToolbar *toolBar;
 IBOutlet UIBarButtonItem *barButton;
 NSArray *fieldsArray;

 fieldsArray = [[NSArray alloc] initWithObjects:birthdate,contNo, breed,homeAddress, coat,color, nil];

-(IBAction)next
 { 
 for (int i=0; i<[fieldsArray count]; i++)
 { if ([[fieldsArray objectAtIndex:i] isEditing] && i!=[fieldsArray count]-1)
   {
         [[fieldsArray objectAtIndex:i+1] becomeFirstResponder];
      if (i+1==[fieldsArray count]-1)
        {
            [barButton setTitle:@"Done"];
            [barButton setStyle:UIBarButtonItemStyleDone];
        }else
        {
            [barButton setTitle:@"Done"];
            [barButton setStyle:UIBarButtonItemStyleBordered];
        }
        break;
            }
    }
 }

-(IBAction) previous
{
for (int i=0; i<[fieldsArray count]; i++)
 {
   if ([[fieldsArray objectAtIndex:i] isEditing] && i!=0)
    {
        [[fieldsArray objectAtIndex:i-1] becomeFirstResponder];
        [barButton setTitle:@"Done"];
        [barButton setStyle:UIBarButtonItemStyleBordered];
        break;
    }
  }
 } 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{
[textField setInputAccessoryView:toolBar];
for (int i=0; i<[fieldsArray count]; i++)
{
    if ([fieldsArray objectAtIndex:i]==textField)
    {
        if (i==[fieldsArray count]-1)
        {
            [barButton setStyle:UIBarButtonItemStyleDone];
        }
    }
  }
 }

当我尝试使用 tabelview 外部的文本字段时,下一个和上一个工作正常。下一个和上一个不能与 tabelview 内的内部 uitextfield 一起工作。任何人都可以帮我解决

【问题讨论】:

    标签: iphone ipad ios6 uitableview uitextfield


    【解决方案1】:
    #pragma mark - UITextField Delegate
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
        HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview;
        NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell];
    
        if (currentIndexPath.row != [self.questionsArray count] - 1) {
    
            NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
            HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath];
    
            [self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
    
            [nextCell.textField becomeFirstResponder];
        }
    
        return YES;
    }
    

    请尝试以下代码可能对您有所帮助。

    【讨论】:

    • 自定义单元格文件名
    【解决方案2】:

    在表格视图单元格的内容视图中添加文本字段。 将标签设置为 cellforrowatindexpath 中的文本字段,以便标签随着添加而增加。 标签的形式可以是“3+indexpath.row”

    在文本开始编辑找到当前文本字段的标签,保存标签。 在接下来的增量标签中,使用标签查找文本字段并使其成为第一响应者。

    或试试这个链接How to activate next UITextField in a UITableView? Fabiano Francesconi 提供了很好的答案 http://blog.encomiabile.it/2013/02/08/how-to-activate-next-uitextfield-in-uitableview-ios/

    【讨论】:

      【解决方案3】:

      这是一篇旧帖子,但页面排名很高,所以我会加入我的解决方案。

      我遇到了类似的问题,最终创建了UIToolbar 的子类来管理动态 tableView 中的下一个/上一个/完成的功能,其中包含以下部分:https://github.com/jday001/DataEntryToolbar

      您将工具栏设置为文本字段的inputAccessoryView 并将它们添加到其字典中。这使您可以前后循环浏览它们,即使是动态内容。如果您想在 textField 导航发生时触发自己的功能,可以使用委托方法,但您不必处理管理任何标签或第一响应者状态。

      GitHub 链接上有代码 sn-ps 和示例应用程序,可帮助了解实现细节。您将需要自己的数据模型来跟踪字段内的值。

      【讨论】:

        猜你喜欢
        • 2011-07-08
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多