【问题标题】:Switching between UITextField cells in UITableView在 UITableView 中的 UITextField 单元格之间切换
【发布时间】:2012-10-30 08:39:22
【问题描述】:

非常初学者的 obj-c 问题。

我已将 TableView 与两个部分 http://uaimage.com/image/c6c9ca23 分组。在第一部分中,我有自定义单元格,其中包含UITextField。我需要使用附加按钮“Next”、“Prev”(在第一部分的文本字段之间切换)和“Done”(关闭键盘)http://uaimage.com/image/62f08045 为键盘实现 Input Accessory View。

问题是:我需要什么来实现通过点击输入附件按钮在 TableView 第一部分的单元格中的文本字段之间切换的可能性? 我是否需要标记单元格或文本字段,如果需要,当用户点击输入附件按钮时如何检索它们的值?或者这是一种更好的方法?

谢谢,亚历克斯

【问题讨论】:

    标签: iphone objective-c xcode cocoa-touch uitableview


    【解决方案1】:

    我假设你有 3 个 UITextField 即 txt ,txt1, txt 2 和 tags 0 1 和 2;现在在 .h 文件中添加UITableViewCell *cell

    编辑: 现在要从当前 tableView 单元格中获取所有 textField 的引用,请添加此委托方法:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        cell = nil;
        cell = (UITableViewCell *)[textField superView];
        return YES;
    }
    

    现在在输入附件 prev 按钮操作中执行以下操作:

    -(IBAction)previousBtn:(id)sender
    {
       UITextField *txt = (UITextField*)[cell viewWithTag:0];
       UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
       UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
       if(txt.isFirstResponder)
       {
        [txt resignFirstResponder];
        [txt2 becomeFirstResponder];
       }
       else if(txt1.isFirstResponder)
       {
        [txt1 resignFirstResponder];
        [txt becomeFirstResponder];
       }
       else if(txt2.isFirstResponder)
       {
        [txt2 resignFirstResponder];
        [txt1 becomeFirstResponder];
       }
    }
    

    现在在 Input Accessory 的下一个按钮操作中执行以下操作:

    -(IBAction)nextBtn:(id)sender
    {
       UITextField *txt = (UITextField*)[cell viewWithTag:0];
       UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
       UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
    
       if(txt.isFirstResponder)
       {
        [txt resignFirstResponder];
        [txt1 becomeFirstResponder];
       }
       else if(txt1.isFirstResponder)
       {
        [txt1 resignFirstResponder];
        [txt2 becomeFirstResponder];
       }
       else if(txt2.isFirstResponder)
       {
        [txt2 resignFirstResponder];
        [txt becomeFirstResponder];
       }
    }
    

    【讨论】:

    • 如果我正确理解了这段代码,我需要以编程方式为所有文本字段编写属性吗?
    • 不需要从 tableView 中的当前 UITableViewCell 中获取所有 txt、txt1、txt2 的引用
    • 对不起,还有一个问题要编码。除了 -cellForRowAtIndexPath: 以外的任何地方都没有声明单元格。我是否需要声明它(或设置属性)才能在 -textFieldShouldBeginEditing: 中使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多