我使用一种方法以编程方式创建 12 个文本字段,并为每个文本字段添加一个标签。 下面是创建文本字段的方法,该方法被调用 12 次来创建每个文本字段。这有效。
textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = name;
[textField setTag:tag];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];
我的问题是如何在每个文本字段中设置文本或如何使用标签访问它。
我尝试过:
[textField.2 setText: [NSString stringWithFormat : @"HEY"]];
这不起作用。
最佳答案
[(UITextField *)[self.view viewWithTag:2] setText:@"foobar"];
创建一个包含文本字段的 NSArray
属性通常是更好的主意。
关于ios - 如何在带有标签的 TextField 中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594521/