动态创建时如何获取 UITextField 值
我通过以下代码动态创建文本字段:
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 处等等。有什么想法吗?
I am creating textField dynamically by the following code:
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];
}
Now i want to get value entered in textfields.
I came to know about delegate protocol for UITextField.
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"Dic: %@",textField.text);
}
But I am not able to implement how to store this values at index they were created, like i want first textField to be stored at index 0 of array and so on. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用每个 UIView 子类都有的
tag
属性:PS 由于 0 是标记属性的默认值,因此最好调整标记以使它们从任意非零值开始。
Use
tag
property which every UIView subclass has:P.S. Since 0 is default value for a tag property it may be better to adjust tags to make them start from arbitrary non-zero value.