嗨,我正在开发一款 iPhone 应用程序,我在其中填充了 uipickerview,它与一个 uitextfield 配合使用效果很好。但我需要填充 6 个 uitextfield,并且我为每个 UItextfield 使用相同的 uipickerview。
我正在使用加载了数组对象的 Ipickerview,并且当触摸每个字段时它会弹出。问题在于 UItextfields 下面的代码共享来自选择器的相同数据。
我不知道如何编码,以便每个字段从 UIPickerView 行获取自己的数据。 我究竟做错了什么?有编码建议吗?
谢谢
@实现BestViewController
-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView
{ return 1; }
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
{return [list count]; }
-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component
{return [list objectAtIndex:row]; }
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
uitextfield1.text = [list objectAtIndex:row];
utitextfield2.text = [list objectAtIndex:row];
}
- (void)viewDidLoad {
[super viewDidLoad];
uitextfield1.inputView = pickerView;
uitextfield2.inputView = pickerView;
list = [[NSMutableArray alloc] init];
[list addObject:@"a"];
[list addObject:@"b"];
[list addObject:@"c"];
[list addObject:@"d"];
}
最佳答案
您需要获取当前的第一响应者并设置其 text
属性,而不是显式设置特定的文本字段。
所以,
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
// textFields is an NSArray holding each of the textfields that are using the picker as an input view
for (UITextField textField in textFields)
{
if ([textField isFirstResponder])
{
textField.text = [list objectAtIndex:row];
break;
}
}
}
可能有一种更优雅的方式来找到当前的第一响应者,这超出了我的想象。 textFields
可能是一个 IBOutletCollection,但我自己没有使用过它们,所以我不能有太多权威。
关于iphone - UIpickerView 在同一个 ScrollView 中填充多个 uitextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139789/