0

我有一个 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

谢谢你的帮助

4

2 回答 2

1

你应该做的是:

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

你的 plist 文件是一个 NSDictionary;

于 2011-07-13T15:06:56.680 回答
1

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

首先,使数组成为在 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 的结构是什么?- 它只是一个平面数组,还是分配给一键字典的数组,还是带有键/对象对的字典......

于 2011-07-13T15:08:25.553 回答