【问题标题】:insert UIPicker selected value into UITextField将 UIPicker 选择的值插入 UITextField
【发布时间】:2013-11-25 11:36:24
【问题描述】:

我试图弄清楚如何将选定的UIPicker 值传递给UITextField。我已经创建了选择器和几个UItextFields.tag 来标识UITextField 将值放入其中,但是我只是不知道该怎么做。

这是我在点击UIPickerView 时使用的方法

// Do something with the selected row.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"You selected this: %@", [dataArray objectAtIndex: row]);

    NSString *temp = [dataArray objectAtIndex:row]; // this contains the selected value from UIPickerView
    NSLog(@"%@", temp);

//    if (cutField.tag == 0) { // trying to pass the string to the correct UItextfield... or any UItextfield for that matter
        cutField.text = temp;
//    }

}

上述方法已执行,但在 cutField 中从未设置值。我不知道如何确定应该更新哪一个,因为我不知道如何访问标签值。

这就是我分配UITextField 的标记值的方式:

for (int i = 0; i < cutCount; i++) 
{
      //first one
      cutField = [[UITextField alloc] initWithFrame:CGRectMake(((positions*i)-(20/2)+(positions/2)), 25, 20, 20)];
      cutField.inputView = pickerView;
      cutField.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
      cutField.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
      cutField.backgroundColor=[UIColor whiteColor];
      [view addSubview:cutField];

      cutField.tag = tagNumber;
      tagNumber ++;

      [columnArrayOfTextFields addObject:cutField]; // array of textfields
}

【问题讨论】:

    标签: ios tags uitextfield uipickerview


    【解决方案1】:

    保留对包含所有这些“cutFields”的超级视图的引用。在下面的示例中,我将其命名为 containerView。此外,您的示例使用 '0' 作为标签,所以我在下面也使用了它。虽然我会假设你也会为此使用一个变量。

    然后使用:

    ((UITextField *)[self.containerView viewWithTag:0]).text = temp;
    

    或分散在多行:

    UITextField* textField = (UITextField*) [self.containerView viewWithTag:0];
    textField.text = temp;
    

    【讨论】:

    • 我收到一个错误,最后一个方括号中的预期标识符。
    • 此代码包含和错误.. 试图自己弄清楚但很难在网上找到任何参考
    • @HurkNburkS 抱歉,我实际上并没有检查代码是否已编译,只是我的转换范围括号错误。 viewWithTag 仅返回基类 UIView,因此您必须在访问 text 属性之前进行强制转换。
    • 好吧,这适用于将文本输入到 UItextfield,但由于某种原因,它没有将文本输入到我使用 .tag 定义的文本字段中。
    【解决方案2】:

    您刚刚实例化了一个UITextField,所以cutField.tagcutCount-1,您可以这样尝试:

    for (int i = 0; i < cutCount; i++) {
                //first one
                UITextField *cutField = [[UITextField alloc] initWithFrame:CGRectMake(((positions*i)-(20/2)+(positions/2)), 25, 20, 20)];
                cutField.inputView = pickerView;
                cutField.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
                cutField.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
                cutField.backgroundColor=[UIColor whiteColor];
                [view addSubview:cutField];
    
                cutField.tag = tagNumber;
                tagNumber ++;
    
    
    
                [columnArrayOfTextFields addObject:cutField]; // array of textfields
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多