0

我有一個基於核心數據的應用程序,我正在使用它,它使用EditingViewController來控制許多不同的UI元素,這些元素從用戶處獲得輸入,這些輸入描述隨後存儲在我的應用程序中的對象的屬性。 EditingViewController.xib文件具有諸如datePicker,textField,numberSlider和我的問題來自UIPickerView的所有元素,這些元素都使用.hidden = YES/NO;表達式在一個視圖中進行控制。我的問題是我需要在兩個單獨的屏幕中填充一個UIPickerView,它需要兩個不同的NSMutableArray來源。在我的viewDidLoad方法中設置我的第一個的NSMutableArray:UIPickerView與2個NSMutableArrays?

listOfItems = [[NSMutableArray alloc] init]; 
[listOfItems addObject:@"Greenland"]; 
[listOfItems addObject:@"Switzerland"]; 
[listOfItems addObject:@"Norway"]; 
[listOfItems addObject:@"New Zealand"]; 
[listOfItems addObject:@"Greece"]; 
[listOfItems addObject:@"Rome"]; 
[listOfItems addObject:@"Ireland"]; 

在這之後,我填充我UIPickerView *選擇器使用此代碼:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker; 
{ 
    return 1; 
} 

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component 
{ 
    label.text= [listOfItems objectAtIndex:row];// The label is simply setup to show where the picker selector is at 
} 

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component; 
{ 
    return 8;//[listOfItems count]; 

} 

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; 
{ 
    return [listOfItems objectAtIndex:row]; 
} 

這對第一屬性和陣列工作正常。那麼之後,當選擇不同的uitableviewcell時,選擇器被隱藏picker.hidden = YES;,我需要另一個選擇器,picker2顯示不同的信息數組。但是當我嘗試並複製過程時,通過設置一個全新的選擇器,將其稱爲picker2,並試圖用我在第一個旁邊創建的另一個NSMutableArray填充它(再次,因爲在我的EditingViewController中它全部是隻是根據視圖調用不同的UI元素)我不能讓picker2填充新的數組。我不知道如何設置它,以便我的兩個不同的陣列可以填充。我需要兩個選擇器視圖嗎?它可以只用一個來完成嗎?在- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;方法中使兩個數組單獨可見的正確語法是什麼?希望有人能幫我解決這個問題,提前謝謝!

回答

3

您要求使用與兩個選取器的代表相同的對象。

在每個委託方法中,爲此提供了選擇器作爲第一個參數。您可以檢查哪個選擇器已通過並返回該選取器的相應數據。

例如,如果你添加一個屬性名爲「pickerOne」第一選擇器和您第二可變數組被稱爲「arrayTwo」,委託方法是這樣的:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker; 
{ 
    return 1; // assuming both pickers only have 1 component 
} 

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component 
{ 
    // The label is simply setup to show where the picker selector is at 
    if (picker == self.pickerOne) { 
     label.text= [listOfItems objectAtIndex:row]; 
    } else { 
     label.text= [arrayTwo objectAtIndex:row]; 
    } 
} 

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component; 
{ 
    if (picker == self.pickerOne) { 
     return [listOfItems count]; 
    } else { 
     return [arrayTwo count]; 
    } 
} 

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; 
{ 
    if (picker == self.pickerOne) { 
     return [listOfItems objectAtIndex:row]; 
    } else { 
     return [arrayTwo objectAtIndex:row]; 
    } 
} 

此外,您還可以像這樣填充你的數組(如果你只是有一個靜態的字符串列表,這個數組並不需要是可變的):

listOfItems = [[NSArray alloc] initWithObjects:@"Baptisms",@"Greenland",@"Switzerland",@"Norway",@"New Zealand",@"Greece",@"Rome",@"Ireland",nil]; 
+0

哇,謝謝!那太完美了! – Steve 2010-01-19 11:07:09