0

我有一个大问题,几天都解决不了。

当应用程序第一次加载时,我在第一个 UIViewController 中将 4 种颜色保存到 NSUserDeafaults。

视图控制器.m

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSUserDefaults *sharedDefaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger countValue = [defaults integerForKey:@"Array"];

if ([sharedDefaults boolForKey:@"FirstLaunch"])
{
    [self openSubView];
    [sharedDefaults setBool:NO forKey:@"FirstLaunch"];
    [sharedDefaults synchronize];
    [self saveColorsToDefaults];

    // [prefs synchronize];
}   // Do any additional setup after loading the view.
else if(countValue == 1)
{
}

}


-(void)saveColorsToDefaults{

const CGFloat  *components1 = CGColorGetComponents([UIColor darkGrayColor].CGColor);
const CGFloat  *components2 = CGColorGetComponents([UIColor blueColor].CGColor);
const CGFloat  *components3 = CGColorGetComponents([UIColor redColor].CGColor);
const CGFloat  *components4 = CGColorGetComponents([UIColor purpleColor].CGColor);

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setFloat:components1[0]  forKey:@"cr"];
[prefs setFloat:components1[1]  forKey:@"cg"];
[prefs setFloat:components1[2]  forKey:@"cb"];
[prefs setFloat:components1[3]  forKey:@"ca"];

[prefs setFloat:components2[0]  forKey:@"cr2"];
[prefs setFloat:components2[1]  forKey:@"cg2"];
[prefs setFloat:components2[2]  forKey:@"cb2"];
[prefs setFloat:components2[3]  forKey:@"ca2"];

[prefs setFloat:components3[0]  forKey:@"cr3"];
[prefs setFloat:components3[1]  forKey:@"cg3"];
[prefs setFloat:components3[2]  forKey:@"cb3"];
[prefs setFloat:components3[3]  forKey:@"ca3"];

[prefs setFloat:components4[0]  forKey:@"cr4"];
[prefs setFloat:components4[1]  forKey:@"cg4"];
[prefs setFloat:components4[2]  forKey:@"cb4"];
[prefs setFloat:components4[3]  forKey:@"ca4"];

[prefs synchronize];
NSLog(@"I just saved colors");


}

在我需要将这些颜色设置为 `DrawViewControllers 绘图工具之后。我只能得到最后 3 种颜色,但第一种颜色没有出现。

但是当我通过设置第一个仪器colorPicker的颜色并将所有颜色再次保存到NSUserDefaults它时效果很好!

 -(void)LoadColorsAtStart
{

NSUserDefaults *prefers = [NSUserDefaults standardUserDefaults];
UIColor* tColor = [UIColor colorWithRed:[prefers floatForKey:@"cr"] green:[prefers floatForKey:@"cg"] blue:[prefers floatForKey:@"cb"] alpha:[prefers floatForKey:@"ca"]];

UIColor* tColor2 = [UIColor colorWithRed:[prefers floatForKey:@"cr2"] green:[prefers floatForKey:@"cg2"] blue:[prefers floatForKey:@"cb2"] alpha:[prefers floatForKey:@"ca2"]];

UIColor* tColor3 = [UIColor colorWithRed:[prefers floatForKey:@"cr3"] green:[prefers floatForKey:@"cg3"] blue:[prefers floatForKey:@"cb3"] alpha:[prefers floatForKey:@"ca3"]];

UIColor* tColor4 = [UIColor colorWithRed:[prefers floatForKey:@"cr4"] green:[prefers floatForKey:@"cg4"] blue:[prefers floatForKey:@"cb4"] alpha:[prefers floatForKey:@"ca4"]];

[prefers synchronize];

[self extractRGBforBlack:tColor];
[self extractRGBforBlue:tColor2];
[self extractRGBforRed:tColor3];
[self extractRGBforLine:tColor4];

[self.colorBar1 setTextColor:self.blackExtract];
[self.colorBar2 setTextColor:self.blueExtract];
[self.colorBar3 setTextColor:self.redExtract];
[self.colorBar4 setTextColor:self.lineExtract];

NSLog(@"I have extracted colors");   
}

我有 5 个类似DrawViewControllers的作品,所有作品都与 Drawing 一起使用UIView

4

2 回答 2

0

试试这个它会正常工作

1.将 UIColor 作为 NSString 存储到 NSUserDefaults 中

-(void)storeColor
{
    NSString *colString = [NSString stringWithFormat:@"%@", [UIColor whiteColor]];
    [[NSUserDefaults standardUserDefaults] setObject:colString forKey:@"keyFontColor"];
}

2.在检索时将字符串转换为UIColor

-(void)retriveColor
    NSArray *values = [[[NSUserDefaults standardUserDefaults] objectForKey:@"keyFontColor"] componentsSeparatedByString:@" "];
    NSLog(@"%d",[values count]);
    UIColor *def_color = [[UIColor alloc] init];
    if ([values count]==5) {
        CGFloat red = [[values objectAtIndex:1] floatValue];
        CGFloat green = [[values objectAtIndex:2] floatValue];
        CGFloat blue = [[values objectAtIndex:3] floatValue];
        CGFloat alpha = [[values objectAtIndex:4] floatValue];
        def_color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
        self.dateLabel.textColor = def_color;

    }
    else if ([values count]==3)
    {
        CGFloat white = [[values objectAtIndex:1] floatValue];
        CGFloat alpha = [[values objectAtIndex:2] floatValue];
        def_color = [UIColor colorWithWhite:white alpha:alpha];
        self.label.textColor = def_color;        

    }
    else
    {
        // its store default value while app loads first time
        self.label.textColor.textColor = [UIColor blackColor];

    }

}
于 2013-12-10T11:42:28.300 回答
0

1.将UIColor作为nsdata存入nsuserdefaults

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];

2.从viewdidload中检索UIColor

UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
于 2013-12-09T08:59:22.240 回答