我有2个不同的视图设置视图和默认视图
我无法将uiswitch值传递给默认视图
切换存储在NSUserDefaults中的位置
settingsview.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"SwitchState"])
self.BlowSwitch.on = [defaults boolForKey:@"SwitchState"];
}
- (IBAction)saveSwitchState:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([self.BlowSwitch isOn]){
[defaults setBool:YES forKey:@"SwitchState"];
//[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SwitchState" object:self userInfo:nil];
}
else{
[defaults setBool:NO forKey:@"SwitchState"];
[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SwitchState"
object:self userInfo:nil];
}
}
defaultview.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onAppSettingsChanged:) name:@"SwitchState"
object:nil];
}
- (void) onAppSettingsChanged:(NSNotification*)notification {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL completed = [defaults boolForKey:@"SwitchState"];
if (completed == YES) {
//bla bla bla
}
}
答案 0 :(得分:0)
检查[defaults synchronize]
时,您无法执行[self.BlowSwitch isOn]
,请删除评论然后重试。