gpt4 book ai didi

ios - 为什么我的 textFieldDidEndEditing 标签不起作用?

转载 作者:行者123 更新时间:2023-11-29 03:52:14 27 4
gpt4 key购买 nike

美好的一天!

我有一个 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 占位符来显示其相应的标签,并且工作正常。

屏幕截图:

enter image description here

问题出在我的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

最佳答案

您正在调用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;
}

关于ios - 为什么我的 textFieldDidEndEditing 标签不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com