开发者

From plist to UIPickerView

I have a plist that I want to get the contents of in a uipickerview.

I'm able to output the contents of the plist in the console using:

    // 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);

what I need to figure out is how to get this content into the UIPickerView.

I tried the following:

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

, but get an error: exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instanc开发者_开发知识库e

thanks for any help


What you should do is:

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

Your plist file is an NSDictionary;


You've got to use the picker delegate/datasource

First, make the array a property loaded in the viewDidLoad, i.e.:

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

THEN

Conform your class to picker delegate/datasource, and hook it up in 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];
}

What is the structure of your plist? - Is it just a flat array, or an array assigned to a one-key dict, or a dict with key/object pairs...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜