2011-07-13 7 views
0

私はuipickerviewの内容を取得したいplistを持っています。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]; 

が、エラーを取得:任意のヘルプあなたが何をすべき

答えて

1

ためexception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance

おかげである:

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

あなたのplistファイルはNSDictionaryです。あなたはすなわち、配列にのviewDidLoadにロードされたプロパティを作成し、ピッカーのデリゲート/データソース

ファーストを使うために持って

1

-(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の構造は何ですか? - それは単純な配列、または1キーdictに割り当てられた配列、またはキー/オブジェクトのペアを持つdict ...

+0

plistは、辞書と各項目の辞書内の4つの文字列を持つ配列です。私はあなたが上記のものと同様のものを試しましたが、エラー "myArray"を取得し続けました。 – hanumanDev