我通过以下代码动态创建textField:
for (int i=0; i<count_size; i++) {
//UITextfield in UIview
CGRect myTextField = CGRectMake(8, 5, 60, 30);
UITextField *txtField = [[UITextField alloc] initWithFrame:myTextField];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setTextColor:[UIColor blackColor]];
[txtField setFont:[UIFont systemFontOfSize:20]];
[txtField setDelegate:self];
txtField.returnKeyType = UIReturnKeyNext;
[txtField setPlaceholder:@"0"];
txtField.keyboardType = UIKeyboardTypeNumberPad;
[myFirstView addSubview:txtField];
}
现在我想在文本字段中输入值。
我开始了解UITextField的委托协议。
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"Dic: %@",textField.text);
}
但我无法实现如何将这些值存储在创建它们的索引中,就像我希望第一个textField存储在数组的索引0一样,依此类推。有什么想法吗?
答案 0 :(得分:1)
使用每个UIView子类具有的tag
属性:
for (int i=0; i<count_size; i++) {
//UITextfield in UIview
CGRect myTextField = CGRectMake(8, 5, 60, 30);
UITextField *txtField = [[UITextField alloc] initWithFrame:myTextField];
txtField.tag = i;
...
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
int index = textField.tag;
// Now save text using that index
NSLog(@"Dic: %@",textField.text);
}
P.S。由于0是标记属性的默认值,因此最好调整标记以使它们从任意非零值开始。