
[英]UIpickerView populating multiple uitextFields in the same viewcontroller
[英]UIpickerView populating multiple uitextFields in the same ScrollView
嗨,我正在开发一个iphone应用程序,其中我已经填充了uipickerview,它与一个uitextfield一起使用效果很好。 但是我需要填充6个uitextfield,并且每个UItextfield使用相同的uipickerview。
我正在使用加载有数组对象的Ipickerview,当触摸每个字段时它会弹出。 问题在于UItextfields下面的代码共享来自选择器的相同数据。
我不知道如何编码,以便每个字段都从UIPickerView行获取自己的数据。 我究竟做错了什么? 有什么编码建议吗?
谢谢
@implementation 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,但我自己没有使用过,所以我不能说太多话。
这是我的方法:
首先,定义几个不同的选择器。 当然,您使用相同的UIPickerView
但是您更改了一个属性来帮助您区分它们。 (几乎)每个文本字段都有不同的数据。 Apple专门为此目的设计的一个便利属性是tag
,它是每个UIView
可以使用的任意整数。 您可以将相同的标签分配给UITextField
。
例如:
#define kFirstTextField 101
#define kSecondTextField 102
#define kThirdTextField 103
//... etc
在触摸文本字段的方法中:
[myPickerView setHidden:NO]; // or however you show the picker
[myPickerView setTag:textField.tag];
[myPickerView reloadAllComponents];
在选择器的数据方法中:
-(NSString *)pickerView:(UIPickerView *)thePickerview
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (thePickerView.tag==kFirstTextField)
{ return [list1 count]; }
if (thePickerView.tag==kSecondTextField)
{ return [anotherOrTheSameList count]; }
// of course, you can also use a switch statement
return [defaultList count];
}
在titleForRow:
方法中执行类似的titleForRow:
。
最后,当某物被拾取时,通过标签再次区分:
-(void) pickerView:(UIPickerView *)thePickerview
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];
// this assumes the UIPickerView is the subview of the same view as the UITextFields
if (thePickerview.tag==kFirstTextField)
{ field.text = [list1 objectAtIndex:row]; }
if (thePickerview.tag==kSecondTextField)
{ field.text = [anotherOrTheSameList objectAtIndex:row]; }
// etc.
// alternatively:
field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.