gpt4 book ai didi

iphone - 在同一个类中使用两个 UIPickerView

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:30 25 4
gpt4 key购买 nike

我为第一个 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];
}

然后我想用 cities 制作另一个 UIPickerView。这是我写的

 citys.delegate = self;
citys.dataSource = self;
citys.showsSelectionIndicator = YES;
cityField.inputView=citys;

但是当我点击它时,我有国家列表。我应该如何更改数据源?以及如何使用 UIPickerView 的默认功能,如 numberOfComponentsInPickerView 、 numberOfRowsInComponent: ... 和第二个 UIPickerView ?

最佳答案

您可以为您的 pickerviews 分配标签,然后可以在数据源/委托(delegate)方法中检查这些标签

citysPickerview.tag = 2

otherPickerview.tag = 1


// then you can check for these tags in pickerview datasource/delegate methods like this -

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

NSString *title;

if (pickerview.tag == 1) // this is otherPickerview
{
title=[countriesArray objectAtIndex:row]; // your logic to get title for otherpickerview


}
else if (pickerview.tag == 2) // this is citysPickerview
{
title=[countriesArray objectAtIndex:row]; // your logic to get title for cityspickerview


}

return title;

}

您应该在所有数据源/委托(delegate)代码中遵循相同的机制:)

关于iphone - 在同一个类中使用两个 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8435969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com