gpt4 book ai didi

ios - NSUserDefaults 机会 UISwitch 值

转载 作者:行者123 更新时间:2023-12-01 16:40:36 24 4
gpt4 key购买 nike

我有 2 个不同的 View 设置 View 和默认 View

我无法将 uiswitch 值传递给默认 View

开关在 NSUserDefaults 中存储位置

设置 View .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];
}
}

默认 View .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
}
}

最佳答案

你不执行[defaults synchronize]检查时[self.BlowSwitch isOn] ,删除评论,然后重试。

关于ios - NSUserDefaults 机会 UISwitch 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com