1

As you can see from the screenshot below I have a countdown clock that I am allowing the user to change the color of buttons and labels on the UI. My problem is, is that I want the user to be able save their color settings, however I cannot figure out placement and order. I understand this question has been answered here: Saving UIColor to and loading from NSUserDefaults.

However, I guess I'm not "getting it" and need the extra help. So each of the colors on the screenshot below have coding like this:

-(IBAction)red
{
    [_enterDateOutlet setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_backButtonOutlet setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _yearsLabel.textColor = [UIColor redColor];
    _daysLabel.textColor = [UIColor redColor];
    _hoursLabel.textColor = [UIColor redColor];
    _minLabel.textColor = [UIColor redColor];
    _secLabel.textColor = [UIColor redColor];
    minuteProgessView.theme.labelColor = [UIColor redColor];
    secondProgessView.theme.labelColor = [UIColor redColor];
    hourProgessView.theme.labelColor = [UIColor redColor];
    dayProgessView.theme.labelColor = [UIColor redColor];
    yearProgessView.theme.labelColor = [UIColor redColor];
}

My question is do I put this code at the end of each Color Action button (I have 7 colors)?

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

Because that's what I figured I was supposed to do, and then I added this to my View Did Load Method:

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

And the result a red error, because "color" was undeclared identifier.

I would really appreciate the help, I'm new to this and I've spent the last 2 hours trying to figure this out. Thank you. Screenshot

Community
  • 1
  • 1
Joshua Hart
  • 772
  • 1
  • 21
  • 31
  • When the use sets the color is that for all the labels? Don't you have to save just the set color and not multiple colors or i am missing something? – Yan Nov 16 '14 at 04:11
  • @Yan It would set all the colors. The main reason being is so that if the user chooses a background photo that causes the countdown to be blinded they can change the color to bring everything back into view. So if the user chooses a background photo and the color yellow works best for them, and they choose yellow, I would like for it to save their choice for the next time they open the app. – Joshua Hart Nov 16 '14 at 04:23
  • So you would only need to save the color of choice and when user opens the app the next time you would retrieve it and set to it. You don't save the rest of the colors – Yan Nov 16 '14 at 04:26

1 Answers1

2

I think you can do something like this. Create a method that will call a function to set color and pass the color to it. In this method u will save the color to user defaults. When the user comes back to the app in viewDidLoad it will retrieve the color and set it. My Xcode is crashing at the moment so i can't run it. Let me know if this is what you are looking for.

-(IBAction)red
{

    [self setAllTo:[UIColor redColor]];
}
-(IBAction)green
    {

        [self setAllTo:[UIColor greenColor]];
    }

-(void)setAllTo:(UIColor *)color
{
    [_enterDateOutlet setTitleColor:color forState:UIControlStateNormal];
        [_backButtonOutlet setTitleColor:color forState:UIControlStateNormal];
        _yearsLabel.textColor = color;
        _daysLabel.textColor = color;
        _hoursLabel.textColor = color;
        _minLabel.textColor = color;
        _secLabel.textColor = color;
        minuteProgessView.theme.labelColor = color;
        secondProgessView.theme.labelColor = color;
        hourProgessView.theme.labelColor = color;
        dayProgessView.theme.labelColor = color;
        yearProgessView.theme.labelColor = color;
        NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
        [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
        [[NSUserDefaults standardUserDefaults] synchronize]         
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
    UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    if(color){
          [self setAllTo:color];
    }else{
         [self setAllTo:[UIColor whiteColor]]; //default color
    }
}
Yan
  • 3,533
  • 4
  • 24
  • 45
  • My friend! Not only did you cut down a lot of coding inside my app, but you have made me very happy! One thing it won't do is load any of the progress.theme.labelColors....if I tap on the color again when in the view it will change the progress theme labels to red, but if I exit and come back in, it's the only thing that isn't staying red. Any ideas? – Joshua Hart Nov 16 '14 at 04:48
  • I usually like to use access variables using properties (self.variableName) and not _ you might have to use _ with all the variables. _minuteProgressView.theme.labelColor etc... Let me know if that works – Yan Nov 16 '14 at 04:51
  • 1
    Blah, found the issue, I actually had the label colors defaulting to white below the viewdidload! Yan you're a genius! Thank you very much! I'm gonna memorize your face and if I ever see you in this crazy world I will buy you a beer! Cheers! – Joshua Hart Nov 16 '14 at 04:52
  • 1
    I am glad i was able to help! :)) Have a good one and happy coding! – Yan Nov 16 '14 at 04:52
  • 1
    As you mentioned about setting default color I realized that should have added few more lines to set default color if nothing is saved in user defaults. Otherwise it might crash the program. – Yan Nov 16 '14 at 11:53
  • Awesome catch! In the If/Else Statement you mean setAllColorTo instead of setColorTo correct? – Joshua Hart Nov 17 '14 at 11:45
  • Yes. I updated the answer and change the name of the method to setAlTo i thought the name sounded better. I am not great at thinking up method names :) – Yan Nov 17 '14 at 13:39