这应该将两个 UISwitches 的值保存到 NSUserDefaults,然后根据保存为默认值的值设置加载开关。我已经在这几个小时了,但我似乎无法让它工作。任何帮助将不胜感激。
切换位于我用于设置的视图中。只有两个切换,所以我想使用 NSUserDefaults。切换默认为关闭。我想切换到 ON 并保存该更改,以便我可以根据切换的值更改用户体验。然后,当用户再次打开设置时,我希望他们显示当前保存的状态。
我认为该值已被保存,但当我重新进入设置视图时,它并未应用于开关。
- (IBAction)setBeadVibratorBySwitchState:(id)sender
{
if (setBeadVibrator.selected == YES) {
NSUserDefaults *beadSettings = [NSUserDefaults standardUserDefaults];
[beadSettings setBool:NO forKey:@"beadSwitchStatus"];
[beadSettings synchronize];
}
if (setBeadVibrator.selected == NO) {
NSUserDefaults *beadSettings = [NSUserDefaults standardUserDefaults];
[beadSettings setBool:YES forKey:@"beadSwitchStatus"];
[beadSettings synchronize];
}
NSLog(@"Bead Executed");
}
- (IBAction)setDecadeVibratorBySwitchState:(id)sender
{
if (setDecadeVibrator.selected == YES) {
NSUserDefaults *decadeSettings = [NSUserDefaults standardUserDefaults];
[decadeSettings setBool:NO forKey:@"decadeSwitchStatus"];
[decadeSettings synchronize];
}
if (setDecadeVibrator.selected == NO){
NSUserDefaults *decadeSettings = [NSUserDefaults standardUserDefaults];
[decadeSettings setBool:YES forKey:@"decadeSwitchStatus"];
[decadeSettings synchronize];
}
NSLog(@"Decade Executed");
}
- (void)viewDidLoad
{
[super viewDidLoad];
setBeadVibrator.selected = [[NSUserDefaults standardUserDefaults] boolForKey:@"beadSwitchStatus"];
setDecadeVibrator.selected = [[NSUserDefaults standardUserDefaults] boolForKey:@"decadeSwitchStatus"];
}