繁体   English   中英

从 UITableView 获取 UITextField 文本

[英]get UITextField text from UITableView

我创建了一个自定义 UITableViewCell,它有一个 UIlabel 和一个 UITextField。

在我的 firstViewController 中,我加载并显示自定义表:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    LoginTableCell *cell = (LoginTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (LoginTableCell *) currentObject;
                break;
            }
        }       
    }

然后,当用户点击“登录”时,我想从那里获取 UITextField.text 值并将其存储到字符串中。

- (IBAction)login:(id)sender {

我知道这听起来很简单,但我一直坚持这一点,似乎无法找到正确的答案来解决。

我以为我可以做这样的事情:

NSString *user = LoginTableCell.loginTextField.text;

但这会返回以下错误:

request for member 'text' in something not a structure or union

非常感谢任何帮助

谢谢

山姆

一般来说,你可以这样做。 确保以下代码中的indexPath是具有所需 textField 的行的正确 indexPath。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
LoginTableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *user = cell.loginTextField.text;

在 tableview didSelectRowAtIndexPath 委托中,您必须为文本字段中的内容声明一个变量,并且您必须通过设置 @property @synthesis 在任何您想要的地方声明它并将此字符串用作全局变量。然后您可以在任何地方使用此字符串你想显示,通过设置你的 textfield.text = your-string;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM