2

我试图弄清楚如何将选定的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
}
4

2 回答 2

2

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

然后使用:

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

或分布在多行:

UITextField* textField = (UITextField*) [self.containerView viewWithTag:0];
textField.text = temp;
于 2013-11-13T02:47:25.253 回答
0

你只有一个实例化,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
        }
于 2013-11-13T02:46:43.907 回答