在同一个类中使用两个 UIPickerView
我为第一个 UIPickerView 编写了这段代码
- (void)viewDidLoad
NSURL *url = [NSURL URLWithString:
@"http://localhost:8080/Data/resources/converter.country/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
// countrys = [[UIPickerView alloc] init];
countrys.delegate = self;
countrys.dataSource = self;
countrys.showsSelectionIndicator = YES;
countryField.inputView=countrys;
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *codeCity;
codeCity=[countriesArray objectAtIndex:row];
return codeCity;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [countriesCodeArray count];
}
,然后我想用城市制作另一个 UIPickerView 。我写了这个,
citys.delegate = self;
citys.dataSource = self;
citys.showsSelectionIndicator = YES;
cityField.inputView=citys;
但是当我点击它时,我有国家列表。我应该如何更改数据源?以及如何使用 UIPickerView 的默认功能,例如 numberOfComponentsInPickerView 、 numberOfRowsInComponent: ... 与第二个 UIPickerView ?
I wrote this code for the first UIPickerView
- (void)viewDidLoad
NSURL *url = [NSURL URLWithString:
@"http://localhost:8080/Data/resources/converter.country/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
// countrys = [[UIPickerView alloc] init];
countrys.delegate = self;
countrys.dataSource = self;
countrys.showsSelectionIndicator = YES;
countryField.inputView=countrys;
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *codeCity;
codeCity=[countriesArray objectAtIndex:row];
return codeCity;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [countriesCodeArray count];
}
And then i wanted to make another UIPickerView with cities . I wrote this
citys.delegate = self;
citys.dataSource = self;
citys.showsSelectionIndicator = YES;
cityField.inputView=citys;
But when i click on it i have countries list . How should i change the datasource ? And how to use the default function of the UIPickerView, like numberOfComponentsInPickerView , numberOfRowsInComponent: ... with the second UIPickerView ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将标签分配给您的pickerviews,然后可以在数据源/委托方法中检查这些标签
您应该在所有数据源/委托代码中遵循相同的机制:)
You can assign tag to your pickerviews and then can check these tags in datasource/delegate methods
You should follow this same mechanism in your all datasource/delegate code :)
你可以做的是为2个
UIPickerView
设置tag
,就像这样 -[countryPicker setTag:1]
,使用这些标签来区分2 个选择器视图。What you could do is set
tag
for the 2UIPickerView
, like so -[countryPicker setTag:1]
, use these tags to distinguish between the 2 picker views.对于更简单的解决方案,只需比较 pickerView 指针即可。这样您就可以节省使用标签带来的额外复杂性和维护成本。
注意:此答案基于 Giuseppe 的评论。
For a simpler solution, just compare the pickerView pointer. That way you save the additional complexity and maintenance of using tags.
Note: This answer is based on giuseppe's comment.