Ios 将UIPicker选定值插入UITextField
我正在尝试找出如何将选定的Ios 将UIPicker选定值插入UITextField,ios,tags,uitextfield,uipickerview,Ios,Tags,Uitextfield,Uipickerview,我正在尝试找出如何将选定的UIPicker值传递到UITextField。我用.tag创建了选择器和几个UITextField,以标识要将值放入哪个UITextField中,但是我不知道怎么做 这是我在点击UIPickerView时使用的方法 // Do something with the selected row. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSI
UIPicker
值传递到UITextField
。我用.tag
创建了选择器和几个UITextField
,以标识要将值放入哪个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
}
for(int i=0;i
您刚刚实例化了一个UITextField
,因此cutField.tag
是cutCount-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
}
for(int i=0;i
保留对包含所有这些“剪切域”的superview的引用。在下面的示例中,我将其称为containerView
。另外,您的示例使用“0”作为标记,因此我在下面也使用了它。虽然我假设你也会使用一个变量
然后使用:
((UITextField *)[self.containerView viewWithTag:0]).text = temp;
或分布在多条线路上:
UITextField* textField = (UITextField*) [self.containerView viewWithTag:0];
textField.text = temp;
我得到一个错误,在最后一个方括号中应为标识符。此代码包含和错误。。我自己想弄清楚,但很难找到任何参考online@HurkNburkS抱歉,我没有检查代码是否编译,我只是选错了范围括号
viewWithTag
仅返回基类UIView
,因此在访问text
属性之前必须强制转换。好的,这在将文本输入UItextfield时有效,但由于某些原因,它不会将文本输入到使用.tag定义的textfield中。