cyw
cyw

Reputation: 540

How can I save the state of a button to NSUserDefaults?

I need help trying to do a crude type of confirmation where you need to type: I got it! into a NSTextField then press button1 so that once they do press button1 I make my button2 enabled using

 -(IBAction)check:(id)sender{
    NSString *string = [NSString stringWithValue:@"I Got It!"];
    if(field.stringValue isEqualToString:string){
      [field setHidden:YES];
      [button1 setHidden:YES];
      [button2 setEnabled:YES]
    }
  }

This is only a one-time confirmation so I'm wondering how I can save the state of the button so that next time they launch the app they don't have to do the confirmation again. The textfield and button1 will be hidden and so that button2 will always be enabled, I want to use the NSUserDefaults because I think that would be the easiest for me to understand.

Upvotes: 1

Views: 267

Answers (2)

Dipen Patel
Dipen Patel

Reputation: 933

You can use as below

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ishidden"] != YES){

// First launch

} else { //not first launch }

Upvotes: 1

Parag Bafna
Parag Bafna

Reputation: 22930

Have a look at NSUserDefaults Class Reference. You can use - (void)setBool:(BOOL)value forKey:(NSString *)defaultName

Upvotes: 1

Related Questions