保存UIButton的隐藏状态

时间:2013-01-01 18:17:25

标签: uibutton nsuserdefaults uiswitch

我有一个UISwitch,在Value Change上,使用if语句隐藏或取消隐藏一些UIButtons。我希望在切换视图或退出应用程序时保存UIButtons的隐藏状态。我尝试过使用NSUserDefaults,但它没有保存,它只是恢复到我最初在.storyboard中设置的状态

.m代码:

- (IBAction)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 viewDidLoad code:

- (void)viewDidLoad {

[super 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];

}

我不知道在哪里可以连接IBAction以使其正常工作。

这是我对UISwitch的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;

    }
}

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

我不确定我是否关注但对我而言,您似乎只缺少对buttonHidden的调用,以便默认实际保存

只是做

-(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];
}