2011-06-13 69 views
0

我想構建將有兩個不同的UIPicker視圖的應用程序。我將它設置爲不同的變量,但是當我運行該應用程序時,我得到了兩個UIPicker具有相同的信息。擁有兩個不同數組的UIPicker的正確方法是什麼?使用多個UIPicker一個屏幕

任何提示?

+0

他們都設置爲使用相同的代表?您將需要單獨的代表來使用不同的數據填充單獨的控件。 – 2011-06-13 16:26:52

+0

@Gregory Hoerner nopes,你可以同時使用一個代表。只要確保你檢查哪一個調用委託方法(使用if語句)。 – 2011-06-13 16:31:02

+0

@WTP,我道歉,我的意思是數據源。 – 2011-06-13 16:34:07

回答

1

只需給每個UIPicker「標籤」

pickerA.tag=0; 
pickerB.tag=1; 

,然後在數據源方法添加:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 

    NSInteger * numberOfComponents; 

    if(picker.tag==0){ 
      numberOfComponents = 3; 
    }else{ 
      numberOfComponents = 4; 
    } 

    return numberOfComponents; 
} 
+0

- (NSInteger)numberOfComponentsInPickerView :(UIPickerView *)pickerViewPort; { \t return 1; } – Brandon 2011-06-13 16:33:05

+0

嗨我已編輯我的答案 – shannoga 2011-06-13 16:45:06

0

那麼你必須有2 uipicker對象類的一個實例變量。

在uipicker數據源和委託方法中,您可以檢查哪些選擇器會調用這些方法,然後相應地在其中加載數據。

但是,如果您沒有uipicker的IBOutlet(如果您已經在xib中創建了它們),那麼只需使用標籤來標識選擇器並適當地加載數據。

+0

我會把我的 - (NSInteger)numberOfCompontsInPicker:......, - (void)pickerview:(UIPickerView)....., - (NSInteger)pickerView :(UIPickervView *)......和 ​​- (NSString *)pickerview:(UIPickerview *)..... – Brandon 2011-06-13 16:34:41

+0

在你的課堂...... – Robin 2011-06-13 16:39:02