我目前有一个UISwitch,使用“if”语句隐藏或取消隐藏一些UIButtons。比开关的ON / OFF状态和按钮的隐藏状态保存在NSUserDefaults中。我想知道如何使该开关出现在设置包中。
这是.m中的“if”语句代码:
- (IBAction)switchTheSwitch:(id)sender {
if (Languages.on) {
china.hidden = NO;
japan.hidden = NO;
french.hidden = NO;
spanish.hidden = NO;
}
else {
china.hidden = YES;
japan.hidden = YES;
french.hidden = YES;
spanish.hidden = YES;
}
[self buttonHidden:sender];
}
现在这是我的代码,它在.m:
中保存了交换机的隐藏状态- (void)buttonHidden:(id)sender {
NSUserDefaults *Defaults = [NSUserDefaults standardUserDefaults];
[Defaults setBool:china.hidden forKey:ChinaChanged];
[Defaults setBool:japan.hidden forKey:JapanChanged];
[Defaults setBool:french.hidden forKey:FrenchChanged];
[Defaults setBool:spanish.hidden forKey:SpanishChanged];
[Defaults synchronize];
}
这是我在.m:
中保存ON / OFF状态的代码- (IBAction)switchChangedValue:(id)sender {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:Languages.on forKey:SwitchChanged];
[userDefaults synchronize];
}
这是我的观点加载:
- (void)viewDidLoad {
NSUserDefaults *Defaults = [NSUserDefaults standardUserDefaults];
china.hidden = [Defaults boolForKey:ChinaChanged];
japan.hidden = [Defaults boolForKey:JapanChanged];
french.hidden = [Defaults boolForKey:FrenchChanged];
spanish.hidden = [Defaults boolForKey:SpanishChanged];
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
Languages.on = [userDefaults boolForKey:SwitchChanged];
}
如何在设置包中制作拨动开关替换视图控制器上的开关?