Save the Hidden State of A UIButton

I have a UISwitch that on Value Change, hides or unhides some UIButtons with an if statement. I would like the hidden state of the UIButtons to be saved when I switch views or exit the app. I have tried using NSUserDefaults but it does not save, it just reverts back to the state I had originally set in the .storyboard

.m code:

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

}

I do not know where I can connect the IBAction to make it work.

This is my if statement code for the UISwitch:

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

    }
}

Can anyone help me.

Answers


im not sure I follow but to me it seems all you lack is a call to buttonHidden so that defaults get actually saved

just do

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

Need Your Help

is it bad to use public static fields/variables?

android memory-leaks static android-memory

I started learning about Android Development, and I read about static variables are bad and may leak memory because they are not garbage collectable.

How does an Enumeration variable works?

java object enums

Consider enum Animals { DOG, FISH, GOAT }; in Java.