2
我有2個不同的視圖設置查看和默認視圖可能有NSUserDefaults的價值UISwitch
我無法通過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
}
}
你確定調用了'onAppSettingsChanged:'嗎? – Stonz2
開關啓用工作正常,但禁用開關沒有任何變化 – SeRcCaN