2014-01-16 32 views
1

我發現了很多關於這個問題的問題,但是所有提出的解決方案似乎都在代表方法中挑選picker1或picker2。如何將兩個不同的DataSources分配給兩個UIPickerView?

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    NSArray *values = (pickerView == picker1 ? values1 : values2); 
    return [values count]; 
} 

然而,似乎有,你可以分配封裝在一個單獨的文件來實現這個兩個不同的數據源,而不必如上述那樣作爲solution b)兩個拾取器之間進行區分的溶液。

這現在有點混亂。假設我有一個ViewController,我使用這兩個選擇器將其設置爲View的FileOwner。現在,如果我創建兩個符合<UIPickerViewDataSource><UIPickerViewDelegate>的獨立接口,那麼如何將視圖中的拾取器綁定到這兩個接口?當然,我可以將它們導入到我的ViewController(FileOwner)中。但那又如何?

回答

0

假設您在xib/storyboard中創建視圖,最簡單的方法就是爲兩個視圖設置網點,並在視圖控制器的viewDidLoad方法中設置數據源/委託。

-(void)viewDidLoad 
{ 
    self.myDataSource1 = [[MyDataSource1 alloc] init]; 
    self.myDataSource2 = [[MyDatasource2 alloc] init]; 

    self.pickerOne.delegate = self.myDataSource1; 
    self.pickerOne.dataSource = self.myDataSource1; 
    self.pickerTwo.delegate = self.myDataSource2; 
    self.pickerTwo.dataSource = self.myDataSource2; 
} 

...其中MyDataSource1和MyDataSource 2是您創建的充當委託的類。

0

可以說MyClass符合選擇器數據源和委託方法。
它也有以下公共財產。

@property NSInteger rows; 
@property NSArray *data; 
.... others properties that you set which you will return in delegate methods. 
在烏爾視圖控制器

現在,

MyClass *delegate1 = [MyClass alloc] init] 
delegate1.rows = numberOfRowsInPicker1; 
delegate1.data = picker1Data; 
// set other public properties of delegate1 
picker1.delegate=delegate1; 
picker1.datasource=delegate1; 

類似delegate2作爲委託picker2。

相關問題