Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

ios - Showing data in picker

Can someone please explain to me why my picker is not showing any data when i load from plist

This is plist:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>England</key>
    <array>
        <string>Chelsea</string>
        <string>Arsenal</string>
    </array>
    <key>Spain</key>
    <array>
        <string>Barca</string>
        <string>Real</string>
    </array>
 </dict>
</plist>` 

This is part of viewController.m

 - (void)viewDidLoad
{
    [super viewDidLoad];
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistFile = [bundle URLForResource:@"myPListFile" withExtension:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistFile];
    self.countryClubs = dictionary;

    NSString *selectedCountry = [self.countries objectAtIndex:0];
    NSArray *array = [countryClubs objectForKey:selectedCountry];
    self.clubs = array;
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0)
        return [self.countries count];
    return [self.clubs count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
        forComponent:(NSInteger)component {
    if (component == 0)
        return [self.countries objectAtIndex:row];
    return [self.clubs objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
       if (component == 0) {
         NSString *selectedCountry = [self.countries objectAtIndex:row];
         NSArray *array = [countryClubs objectForKey:selectedCountry];
         self.clubs = array;
         [picker selectRow:0 inComponent:1 animated:YES];
         [picker reloadComponent:1];
    }
}

in viewController.h i got

 @property (nonatomic, strong)NSDictionary *countryClubs;
@property (nonatomic, strong)NSArray *countries;
@property (nonatomic, strong)NSArray *clubs;

and in viewController.m i got:

@synthesize countryClubs=_countryClubs;
@synthesize countries=_countries;
@synthesize clubs=_clubs;

If anyone could explain why is picker blank i would appreciate it really much, it seems to that plist is not loaded because if i write :

self.clubs  = [[NSArray alloc]      initWithObjects:@"milimetar",@"centimetar",@"metar",@"kilometar",@"inc",@"stopa",@"jard",@"milja", nil];

picker is showing data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your viewDidLoad should be:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistFile = [bundle URLForResource:@"myPListFile" withExtension:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistFile];

    self.countryClubs = dictionary;
    self.countries = [dictionary allKeys];

    NSString *selectedCountry = self.countries[0];
    NSArray *array = self.countryClubs[selectedCountry];
    self.clubs = array;
}

In your didSelectRow: method, change:

NSArray *array = [countryClubs objectForKey:selectedCountry];

to:

NSArray *array = self.countryClubs[selectedCountry];

Also get rid of all of your @synthesize lines. If you are using any tutorial that talks about using @synthesize then it is out of date. Also get rid of any ivars you may have explicitly declared for your properties.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...