在select器中显示数据
有人可以向我解释为什么当我从plist中加载时,我的select器没有显示任何数据
这是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.hi得到
@property (nonatomic, strong)NSDictionary *countryClubs; @property (nonatomic, strong)NSArray *countries; @property (nonatomic, strong)NSArray *clubs;
并在viewController.mi得到:
@synthesize countryClubs=_countryClubs; @synthesize countries=_countries; @synthesize clubs=_clubs;
如果任何人可以解释为什么select器空白,我将非常感激,这似乎是plist不加载,因为如果我写:
self.clubs = [[NSArray alloc] initWithObjects:@"milimetar",@"centimetar",@"metar",@"kilometar",@"inc",@"stopa",@"jard",@"milja", nil];
select器正在显示数据。
你的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。