2011-07-13 29 views
0

我有一個plist,我想要在uipickerview中獲取內容。從plist到UIPickerView

我能夠輸出的plist中使用控制檯的內容:

// Path to the plist (in the application bundle) 
    NSString *path = [[NSBundle mainBundle] pathForResource: 
             @"loa" ofType:@"plist"]; 

    // Build the array from the plist 
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

    // Show the string values 
    for (NSString *str in array2) 
      NSLog(@"--%@", str); 

什麼,我需要搞清楚的是如何得到這個內容到UIPickerView。

我試過如下:

 arrayPicker = [[NSMutableArray alloc] initWithArray:array2]; 

,卻得到一個錯誤:exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance

感謝您的幫助

回答

1

你應該做的是:

NSMutableDictionary *myDataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
NSLog(@"MyDict:%@",[myDataDictionary description]); 

你的plist文件是一個NSDictionary;

1

你必須使用機械臂委託/數據源

首先,在數組中的viewDidLoad中加載的特性,即:

-(void)viewDidLoad { 
NSString *path = [[NSBundle mainBundle] pathForResource: 
             @"loa" ofType:@"plist"]; 
self.myArray = [[NSArray alloc] initWithContentsOfFile:path]; 
} 

THEN

順應你的類選擇器委託/數據源,並在IB中掛鉤。

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
return [myArray count]; 
} 

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

你的plist的結構是什麼? - 是否只是扁平陣列,或分配給一個鍵的dict陣列,或與鍵/對象對的字典...

+0

plist中是用字典並4串爲每個項目Array的字典中。我嘗試了類似於上面的內容,但一直未收到未聲明的錯誤「myArray」。 – hanumanDev