在同一个类中使用两个UIPickerView

时间:2011-12-08 18:43:41

标签: iphone objective-c 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?

3 个答案:

答案 0 :(得分:9)

您可以为您的选择器视图分配标签,然后可以在数据源/委托方法中检查这些标签

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;

}

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

答案 1 :(得分:2)

您可以为2 tag设置UIPickerView,就像这样 - [countryPicker setTag:1],使用这些标记来区分2个选择器视图。

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
        return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
      numberOfRowsInComponent:(NSInteger)component
{
    if([pickerView tag] == 1)
        return [countryNames count];
    else
        return [cityNames count];

}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
    if([pickerView tag] == 1)
        return [countryNames objectAtIndex:row];
    else 
        return [cityNames count];

} 

答案 2 :(得分:0)

对于更简单的解决方案,只需比较pickerView指针即可。这样就可以节省使用标签的额外复杂性和维护。

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger numberOfRows = 0;

    if (pickerView == countrys) {
        numberOfRows = [countriesCodeArray count];
    }
    else if (pickerView == citys) {
        numberOfRows = [citysCodeArray count];
    }

    return numberOfRows;
}

注意:这个答案是基于giuseppe的评论。