在同一个类中使用两个UIPickerView

时间:2020-12-12 12:08:32

I wrote this code for the first 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];
    }

And then i wanted to make another UIPickerView with cities . I wrote this

然后我想做另一个城市的UIPickerView。我写这

 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 ?

但当我点击它时,我有国家列表。我应该如何更改数据源?以及如何使用UIPickerView的默认函数,比如numberOfComponentsInPickerView, numberOfRowsInComponent:…有第二个UIPickerView吗?

3 个解决方案

#1


9  

You can assign tag to your pickerviews and then can check these tags in datasource/delegate methods

您可以为您的pickerviews分配标记,然后在datasource/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;

}

You should follow this same mechanism in your all datasource/delegate code :)

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

#2


2  

What you could do is set tag for the 2 UIPickerView, like so - [countryPicker setTag:1], use these tags to distinguish between the 2 picker views.

您可以做的是为两个UIPickerView设置标记,比如so - [countryPicker setTag:1],使用这些标记来区分两个picker视图。

- (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];

} 

#3


0  

For a simpler solution, just compare the pickerView pointer. That way you save the additional complexity and maintenance of using tags.

要获得更简单的解决方案,只需比较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;
}

Note: This answer is based on giuseppe's comment.

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

#1


9  

You can assign tag to your pickerviews and then can check these tags in datasource/delegate methods

您可以为您的pickerviews分配标记,然后在datasource/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;

}

You should follow this same mechanism in your all datasource/delegate code :)

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

#2


2  

What you could do is set tag for the 2 UIPickerView, like so - [countryPicker setTag:1], use these tags to distinguish between the 2 picker views.

您可以做的是为两个UIPickerView设置标记,比如so - [countryPicker setTag:1],使用这些标记来区分两个picker视图。

- (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];

} 

#3


0  

For a simpler solution, just compare the pickerView pointer. That way you save the additional complexity and maintenance of using tags.

要获得更简单的解决方案,只需比较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;
}

Note: This answer is based on giuseppe's comment.

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