在同一个类中使用两个 UIPickerView

发布于 2024-12-20 12:26:18 字数 1382 浏览 1 评论 0原文

我为第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖 2024-12-27 12:26:18

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

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 can assign tag to your pickerviews and then can check these tags in datasource/delegate methods

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 :)

神魇的王 2024-12-27 12:26:18

你可以做的是为2个UIPickerView设置tag,就像这样 - [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];

} 

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.

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

} 
眸中客 2024-12-27 12:26:18

对于更简单的解决方案,只需比较 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 的评论。

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

- (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文