From plist to 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

感谢您的帮助

你应该做的是:

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

你的plist文件是一个NSDictionary;

你必须使用选择器委托/数据源

首先,让数组成为viewDidLoad中加载的属性,即:

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

使您的类符合选择器委托/数据源,并在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的结构是什么?-它只是一个平面数组,还是一个分配给一键字典的数组,还是一个具有键/对象对的字典…

最新更新