Picker 将随机数据保存到 NSUserDefaults
Picker saves random data to NSUserDefaults
我一直在为一款名为 Dofus 的游戏开发一个新的提示和技巧项目。在第一个视图中,要求用户从选择器中选择 class。然后他们转到下一个视图并选择他们的级别和类似的东西。然而,选择器的问题是它从选择器中得到了错误的 class。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.classNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
return self.classNames[row];
}
在 ViewDidLoad 中我声明了我的 classes:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
self.classNames = @[@"Cra", @"Ecaflip", @"Eliotrope", @"Eniripsa",
@"Enutrof", @"Feca", @"Foggernout", @"Iop", @"Masqueraider", @"Osamodas", @"Pandawa", @"Rogue", @"Sacrier", @"Sadida", @"Sram", @"Xelor"];
}
然后在下一个视图中我得到这样的信息(不同的 .m 文件):
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myClass = [prefs stringForKey:@"class"];
_textLabel.text = [NSString stringWithFormat:@"So you are a %@ huh? Pretty cool! Its a good class. Fill out the rest so we are ready to go!", myClass];
}
有人知道这是怎么回事吗?提前致谢!
您正在为行标题的选择器数据源调用设置首选项,而不是在选择行时的委托调用。对于加载到视图中的每一行,调用 pickerView:titleForRow:forComponent:
,此时您设置 class
首选项,但是,这并不对应于在选择器中选择的行,它对应于要显示的下一行。
相反,响应
pickerView:didSelectRow:inComponent:
方法,并在那里设置您的首选项(您仍然需要响应另一个来设置行标题,只是不要设置首选项)。
- (NSString *)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger) row
inComponent:(NSInteger) component {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
}
如果你想存储用户选择的行,你应该使用 UIPickerViewDelegate
中的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
;
类似的东西会起作用:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
[prefs synchronize];
}
}
查看这些链接以更好地了解 UIPickerView 的工作原理:
http://nscookbook.com/2013/01/ios-programming-recipe-7-using-the-uipickerview/
http://www.techotopia.com/index.php/An_iOS_7_UIPickerView_Example
我一直在为一款名为 Dofus 的游戏开发一个新的提示和技巧项目。在第一个视图中,要求用户从选择器中选择 class。然后他们转到下一个视图并选择他们的级别和类似的东西。然而,选择器的问题是它从选择器中得到了错误的 class。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.classNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
return self.classNames[row];
}
在 ViewDidLoad 中我声明了我的 classes:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
self.classNames = @[@"Cra", @"Ecaflip", @"Eliotrope", @"Eniripsa",
@"Enutrof", @"Feca", @"Foggernout", @"Iop", @"Masqueraider", @"Osamodas", @"Pandawa", @"Rogue", @"Sacrier", @"Sadida", @"Sram", @"Xelor"];
}
然后在下一个视图中我得到这样的信息(不同的 .m 文件):
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myClass = [prefs stringForKey:@"class"];
_textLabel.text = [NSString stringWithFormat:@"So you are a %@ huh? Pretty cool! Its a good class. Fill out the rest so we are ready to go!", myClass];
}
有人知道这是怎么回事吗?提前致谢!
您正在为行标题的选择器数据源调用设置首选项,而不是在选择行时的委托调用。对于加载到视图中的每一行,调用 pickerView:titleForRow:forComponent:
,此时您设置 class
首选项,但是,这并不对应于在选择器中选择的行,它对应于要显示的下一行。
相反,响应
pickerView:didSelectRow:inComponent:
方法,并在那里设置您的首选项(您仍然需要响应另一个来设置行标题,只是不要设置首选项)。
- (NSString *)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger) row
inComponent:(NSInteger) component {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
}
如果你想存储用户选择的行,你应该使用 UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
;
类似的东西会起作用:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
[prefs synchronize];
}
}
查看这些链接以更好地了解 UIPickerView 的工作原理:
http://nscookbook.com/2013/01/ios-programming-recipe-7-using-the-uipickerview/
http://www.techotopia.com/index.php/An_iOS_7_UIPickerView_Example