在选择器中显示数据

时间:2014-01-16 21:24:42

标签: ios plist uipickerview

当有人从plist

加载时,有人可以向我解释为什么我的选择器没有显示任何数据

这是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>` 

这是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];
    }
}

在viewController.h中我得到了

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

并在viewController.m中得到:

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

如果有人能解释为什么选择器空白我会非常感激,似乎plist没有加载,因为如果我写:

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

选择器正在显示数据。

1 个答案:

答案 0 :(得分:0)

您的viewDidLoad应该是:

- (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;
}

didSelectRow:方法中,更改:

NSArray *array = [countryClubs objectForKey:selectedCountry];

为:

NSArray *array = self.countryClubs[selectedCountry];

同时摆脱所有@synthesize行。如果您正在使用任何谈论使用@synthesize的教程,那么它已经过时了。还要删除您可能已为您的属性明确声明的任何ivars。