store -
-(IBAction) setMyColor:(id)sender{
if (sender == yellowButton ) {
[colorView setBackgroundColor:[UIColor yellowColor]];
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
UIColor *strColor = [colorView backgroundColor];
[userDefaults setObject:strColor forKey:@"myColor"];
[userDefaults synchronize];
}
load -
- (void)viewDidLoad {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[colorView setBackgroundColor[UIColor [userDefaults objectForKey:@"myColor"]]];
[super viewDidLoad];
}
but it failed to run ... maybe the question is 'how to get the backgroundColor value from UIView'?
you can use
//save
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
//retrieve
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
or
//save
const CGFloat *components = CGColorGetComponents(pColor.CGColor);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:components[0] forKey:@"cr"];
[prefs setFloat:components[1] forKey:@"cg"];
[prefs setFloat:components[2] forKey:@"cb"];
[prefs setFloat:components[3] forKey:@"ca"];
//retrieve
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
UIColor* tColor = [UIColor colorWithRed:[prefs floatForKey:@"cr"] green:[prefs floatForKey:@"cg"] blue:[prefs floatForKey:@"cb"] alpha:[prefs floatForKey:@"ca"]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With