0

I have added an option to my app to change the background color and textColor of an NSTextField. I set up an NSPopUpButton and based on the selected item in the NSPopUpButton, it changes the colors. I want to save this selection using NSUserDefaults. I am using this method to change the backgroundColor and textColor and it works. How would I save the properties with NSUserDefaults and have it set on start up?

- (IBAction)addBarColor:(id)sender {
    if ([addBarColor.titleOfSelectedItem isEqualToString:@"White"]) {
        addressBar.backgroundColor = [NSColor whiteColor];
        addressBar.textColor = [NSColor blackColor];
    }
    else {
        //default state
        addressBar.backgroundColor = [NSColor redColor];
        addressBar.textColor = [NSColor whiteColor];
    }
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
Sega dude
  • 1,103
  • 3
  • 12
  • 29

1 Answers1

0

First set in your action method like that below:-

Now in this action whenever you set the color, it will save into default color just added here two lines

- (IBAction)addBarColor:(id)sender {
       NSUserDefaults *default=[NSUserDefaults standardUserDefaults];
[default setObject:addBarColor.titleOfSelectedItem forKey:@"selectedColor"];
//Process your code
}

//Now in this just reading the saved color from defaults and then setting into your popup button

-(void)awakeFromNib
{
 NSUserDefaults *default=[NSUserDefaults standardUserDefaults];
NSString *defColor=[default ObjectForKey:@"selectedColor"];
if (defColor)
{
[addBarColor selectItemWithTitle:defColor];
}

}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56