【问题标题】:Why is my textFieldDidEndEditing tag not working?为什么我的 textFieldDidEndEditing 标记不起作用?
【发布时间】:2013-06-06 09:31:47
【问题描述】:

美好的一天!

我有一个UITableView两个单元格 及其对应的UITextField。我通过 for 循环 创建了 UITextField

- (UITextField *)createInformationTextField:(NSString *)createInformationTextField createInformationPlaceholder:(NSString *)createInformationPlaceholder
{
    for (int i = 0; i < 2; i++) {
        informationTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 170, 30)];

        informationTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        informationTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        informationTextField.placeholder = createInformationPlaceholder;
        informationTextField.text = createInformationTextField;
        informationTextField.adjustsFontSizeToFitWidth = YES;
        informationTextField.clearButtonMode = UITextFieldViewModeAlways;

        informationTextField.tag = i;
    }

    return informationTextField;
}

我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *name;
    NSString *address;

    switch (indexPath.section) {
        case 0:
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            switch (indexPath.row) {
                case 0:
                    informationTextField = [self createInformationTextField:name createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];

                    cell.textLabel.text = @"Name";
                    [cell addSubview:informationTextField];
                    break;

                case 1:
                    informationTextField = [self createInformationTextField:address createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];

                    cell.textLabel.text = @"Address";
                    [cell addSubview:informationTextField];
                    break;

                default:
                    break;
            }

            break;

        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"State";
                    break;

                default:
                    break;
            }

            break;

        default:
            break;
    }

    informationTextField.delegate = self;

    return cell;
}

我将UITableView placeholder 设置为显示其对应的标签,并且工作正常。

截图:

问题出在我的textFieldDidEndEditing:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            NSLog(@"Name Field. The tag is %d", textField.tag);
            break;

        case 1:
            NSLog(@"Address Field. The tag is %d", textField.tag);
            break;

        default:
            break;
    }
}

用户在UITextField 中输入内容后,我尝试再次输出它的标签,但这是我看到的

2013-06-06 17:36:53.050 UITableUITextField[20387:c07] Address Field. The tag is 1
2013-06-06 17:36:55.636 UITableUITextField[20387:c07] Address Field. The tag is 1

Apple's Documentation

【问题讨论】:

  • 您可以发布您的代码以添加文本字段
  • @manujmv 里面的cellForRowAtIndexPath?

标签: ios uitableview tags uitextfield


【解决方案1】:

您正在调用 createInformationTextField 方法来创建文本字段。我认为您正在从每个单元格调用此方法。因此,当您调用此方法时,它会进入 for 循环,并且在两个循环之后,它会返回带有标签的文本字段 1 为每个单元格返回。这就是为什么您为每个文本字段都获得标签 1 的原因。尝试从您的 cellForRowAtIndexPath: 方法中调用以下方法

 - (UITextField *)createInformationTextField:(NSString *)createInformationTextField createInformationPlaceholder:(NSString *)createInformationPlaceholder index:(NSIndexPath*)indexPath
    {

        UITextField *informationTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 170, 30)];

        informationTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        informationTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        informationTextField.placeholder = createInformationPlaceholder;
        informationTextField.text = createInformationTextField;
        informationTextField.adjustsFontSizeToFitWidth = YES;
        informationTextField.clearButtonMode = UITextFieldViewModeAlways;

        informationTextField.tag = indexPath.row;

        return informationTextField;
    } 

【讨论】:

    【解决方案2】:

    像这样在 for 循环中将标签设置为文本字段。

    nameTxtFld.tag = 0;
    addressTxtFld.tag = 1;
    

    【讨论】:

    • 嗨。感谢您的快速回复。我修改了上面的问题,并添加了我的 for 循环 语句。再次感谢您!
    • 嗯,请问在哪里?和/或也许也是一个解决方案?
    • 我试过你的代码是正确的我认为可能是这个代码之外的问题。
    猜你喜欢
    • 2022-11-20
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2013-04-21
    • 2016-03-25
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多