views:

281

answers:

1

Hi everyone! In my app, one if my goals is to use UISegmentedControl to choose the bg color of another screen. The problem is that I have tried to have it so that whenever you went to the options screen, the Segmented Control will remember the option you picked when you left the screen. It only remembers ONE OUT OF THE 5 OPTIONS!!! Here is the code in the options screen -


- (IBAction)changecolor:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if(segcolor.selectedSegmentIndex == 0){
        //Red
        UIColor *red = [UIColor redColor];
        NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:red];
        [prefs setObject:colordata forKey:@"ColorKey"];
    }else if(segcolor.selectedSegmentIndex == 1){
        //Yellow
        UIColor *yellow = [UIColor yellowColor];
        NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:yellow];
        [prefs setObject:colordata forKey:@"ColorKey"];
    }else if(segcolor.selectedSegmentIndex == 2){
        //Green
        UIColor *green = [UIColor greenColor];
        NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:green];
        [prefs setObject:colordata forKey:@"ColorKey"];
    }else if(segcolor.selectedSegmentIndex == 3){
        //Blue
        UIColor *blue = [UIColor blueColor];
        NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:blue];
        [prefs setObject:colordata forKey:@"ColorKey"];
    }else if(segcolor.selectedSegmentIndex == 4){
        //Black
        UIColor *black = [UIColor blackColor];
        NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:black];
        [prefs setObject:colordata forKey:@"ColorKey"];
    }
}
The previous method is connected to the 'value changed' method in IB to the segmentedcontrol, segcolor. And also...

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *colordata = [prefs objectForKey:@"ColorKey"];

    if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor redColor])
        segcolor.selectedSegmentIndex = 0;

    if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor yellowColor])
        segcolor.selectedSegmentIndex = 1;

    if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor greenColor])
        segcolor.selectedSegmentIndex = 2;

    if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor blueColor])
        segcolor.selectedSegmentIndex = 3;

    if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor blackColor])
        segcolor.selectedSegmentIndex = 4;
}

Just for reference, Black is the only option that remembers.

Now for in the main page...

- (void)viewWillAppear:(BOOL)animated {
    //---------------------------------------------------------//
    //----------------------BGData-----------------------------//
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *colorData = [prefs objectForKey:@"ColorKey"];
    UIColor *bgcolor = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    self.view.backgroundColor = bgcolor;

    if (bgcolor == [UIColor blackColor]) {
        currentArtist.textColor = [UIColor whiteColor];
        instructlabel.textColor = [UIColor whiteColor];
        currentSong.textColor   = [UIColor whiteColor];
    } else if (bgcolor != [UIColor blackColor]) {
        currentArtist.textColor = [UIColor blackColor];
        instructlabel.textColor = [UIColor blackColor];
        currentSong.textColor   = [UIColor blackColor];
    }
}

Who sees what's wrong?

+1  A: 

Ouch. You cannot compare contents Objective-C objects with ==. Never. Use -isEqual: to compare UIColors.

And, why don't you directly store the segment index into the user defaults (-setInteger:forKey:, -integerForKey:)? Then you don't need to encode and decode the UIColors which is slow and memory consuming.

KennyTM