在同一个类中使用两个UIPickerView [英] use two UIPickerView in the same class

查看:54
本文介绍了在同一个类中使用两个UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然后我想用城市制作另一个UIPickerView.我写了这个

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;

但是,当我单击它时,就会显示国家列表.我应该如何更改数据源?以及如何将UIPickerView的默认功能(如numberOfComponentsInPickerView,numberOfRowsInComponent:...)与第二个UIPickerView一起使用?

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 ?

推荐答案

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

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

这篇关于在同一个类中使用两个UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆