将UIPicker选择的值插入UITextField

时间:2013-11-13 02:29:55

标签: ios tags uitextfield uipickerview

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

保留对包含所有'cutFields'的superview的引用。我在下面的示例中将其称为containerView。另外,您的示例使用'0'作为标记,因此我也使用了以下内容。虽然我会假设你也会使用变量。

然后使用:

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

或分散在多行:

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

答案 1 :(得分: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
        }