【问题标题】:Pass text to UItextfield from UItableViewcell selection从 UItableViewcell 选择将文本传递给 UItextfield
【发布时间】:2013-12-16 12:47:47
【问题描述】:

我正在尝试将文本从 UITableViewCell 选择传递到 UITextField

我的didSelectRowAtIndex 是这样设置的

#pragma mark -- select row
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"%@", selectedCell.textLabel.text);// shows correct values

    currentSelected.text = selectedCell.textLabel.text; // currentSelected is defined below


    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

这是我的表格视图选择,如您所见,我正在尝试将文本传递给 currentSelected UITextField 但是什么都没有发生..

这就是我设置当前选择的方式

#pragma mark - TextField delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    currentSelected = textField;

}

【问题讨论】:

    标签: ios uitableview uitextfield


    【解决方案1】:
    #pragma mark - TextField delegates
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
        currentSelected = textField;
    
    }
    

    用下面的方法替换并确保 textfield delegate 被清除

    #pragma mark - TextField delegates
    - (void)textFieldDidEndEditing:(UITextField *)textField {
    
        currentSelected.text = textField.text;
    
    }
    

    【讨论】:

    • 这给了我一个警告“:不兼容的指针类型从'NSString *'分配给'UITextField *'”
    • 对不起.. textFieldDidEndEditing 方法
    【解决方案2】:

    将字符串保存到视图控制器的属性中,而不是文本字段中。

    然后,一旦您在属性中添加了文本,您就可以根据需要在文本字段中显示它。

    我的猜测是在 didSelectRowAtIndexPath 中,currentSelected 为 nil。

    【讨论】:

    • 感谢您的评论我已经做了更多的工作,现在我得到了 testfieldshouldreturn 的新值,我尝试设置新文本但它甚至没有显示
    • 任何示例将不胜感激。
    • @HurkNburkS,你需要什么样的例子?你不知道如何将字符串从一个控制器传递到下一个控制器吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多