我有一个大问题,几天都解决不了。
当应用程序第一次加载时,我在第一个 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
。