1

I have a feature in my app that allows the user to change the color of the top and bottom bars of the app. But I don't know how to save this color change so that it will stayed changed. I tried using NSUserDefaults but it didn't work. I am using this method to set the colors:

-(IBAction)uiColorGreen:(id)sender{
topBar.tintColor = [UIColor greenColor];
bottomBar.tintColor = [UIColor greenColor];

How would I save the topBar and bottomBar color attributes so that the chosen color will be applied on start up of the app?

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Sega dude
  • 1,103
  • 3
  • 12
  • 29

2 Answers2

4

As others say you can save as data using NSUserDefaults like this:

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"color1"];

and to get it:

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
lightspeed
  • 122
  • 5
0

Well I am not sure how you tried to save to UserDefaults, but converting your UIColor to NSData and then store in UserDefaults works. You can see this post marked answer for whole source code implementation: Saving UIColor to and loading from NSUserDefaults.

Community
  • 1
  • 1
GUL
  • 207
  • 1
  • 8