UIpickerView 在同一个 ScrollView 中填充多个 uitextFields
你好,我正在开发一个 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"];
}
Hi I am developing an iphone app where I have populated a uipickerview and it works great with one uitextfield. But I have 6 uitextfield I need to populate and I am using the same uipickerview for each UItextfield.
I have working the Ipickerview loaded with array objects and it pops up when each field is touched. The problem is with the code below the UItextfields share the same data from the picker.
I can not figure out how to code so each field gets it own data from the UIPickerView row.
What Am I doing wrong? Any coding suggestions?
Thanks
@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"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取当前的第一响应者并设置其
text
属性,而不是显式设置特定的文本字段。因此,
可能有一种更优雅的方式来找到当前的第一响应者,这超出了我的想象。
textFields
可能是一个 IBOutletCollection,但我自己没有使用过它们,所以我不能有太多权威。You need to get hold of the current first responder and set its
text
property rather than explicitly setting a particular text field.So,
There may be a more elegant way to find the current first responder, this is off the top of my head.
textFields
could possibly be an IBOutletCollection, but I haven't used those myself so I can't speak with much authority.我将这样做:
首先,定义几个不同的选择器。当然,您使用相同的
UIPickerView
但您更改了一个属性来帮助您区分它们。 (几乎)每个文本字段都有不同的数据。 Apple 专门为此目的设计的一个方便的属性是tag
,它是每个UIView
可用的任意整数。您可以将相同的标签分配给UITextField
。例如:
在触摸文本字段时的方法中:
在选择器的数据方法中:
在
titleForRow:
方法中执行类似的操作。最后,当某个东西被选中时,再次通过标签进行区分:
Here is how I would do it:
First, define several different pickers. Of course you use the same
UIPickerView
but you change one property that helps you distinguish them. There is different data for (almost) each text field. One convenient property that Apple designed exactly for this purpose is thetag
, an arbitrary integer available to everyUIView
. You can assign the same tags to theUITextField
s.For example:
In the method when the text field is touched:
In the data methods of the picker:
Do something similar in the
titleForRow:
method.Finally, when something is picked, discriminate again by tag: