奇怪的UISegmentedControl问题!

时间:2010-02-17 15:11:49

标签: iphone uisegmentedcontrol

在我的应用程序中,如果我的目标是使用UISegmentedControl来选择另一个屏幕的bg颜色。问题是我试图让它,以便每当你进入选项屏幕时,分段控件将记住你离开屏幕时选择的选项。它只会记住5个选项中的一个!这是选项屏幕中的代码 -


- (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"];
    }
}
上一个方法与IB中的'value changed'方法连接到segmentedcontrol,segcolor。而且......

- (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;
}

仅供参考,Black是唯一记得的选项。

现在在主页面中......

- (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];
    }
}

谁看到了什么问题?

1 个答案:

答案 0 :(得分:4)

哎哟。您无法将内容Objective-C对象与==进行比较。决不。 Use -isEqual: to compare UIColors

并且,为什么不直接将段索引存储到用户默认值(-setInteger:forKey:-integerForKey:)?然后,您不需要对速度慢且占用内存的UIColors进行编码和解码。