动态创建时如何获取 UITextField 值

发布于 2024-10-17 15:19:53 字数 874 浏览 2 评论 0原文

我通过以下代码动态创建文本字段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-10-24 15:19:53

使用每个 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);
}

PS 由于 0 是标记属性的默认值,因此最好调整标记以使它们从任意非零值开始。

Use tag property which every UIView subclass has:

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文