我尝试使用此代码在objective-c中保存用户从Plain Segmented Control中选择颜色的内容,但它无法正常工作
用于保存
UIColor *saveColor = changeColor.backgroundColor;
NSUserDefaults *Color = [NSUserDefaults standardUserDefaults];
[Color setObject:saveColor forKey:@"saveColor"];
[Color synchronize];
用于加载
NSUserDefaults *color = [NSUserDefaults standardUserDefaults];
UIColor *loadColor = [color objectForKey:@"saveColor"];
[changeColor setBackgroundColor:loadColor];
答案 0 :(得分:0)
感谢我的工作,我用它来保存
smith_welding_000000.jpg
smith_welding_000001.jpg
smith_welding_000002.jpg
这将加载
UIColor *color = self.view.backgroundColor ;
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];
NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
[userDefaults setObject:colorAsString forKey:@"color"];
答案 1 :(得分:0)
您不能直接将UIColor保存到NSUserDfaults。它们必须是NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary的实例,对于可以通过归档放入nsdata的其他和自定义对象。
//For Encoding
UIColor *bgColor =[UIColor whiteColor];
NSData *encodedData =[NSKeyedArchiver archivedDataWithRootObject: bgColor];
[[NSUserDefaults standardUserDefaults] setObject:encodedData forKey:@"bgColorKey"];
//For decoding
NSData *decodedData = [[NSUserDefaults standardUserDefaults] objectForKey:@"bgColorKey"];
UIColor *bgcolor = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
self.yourView.backgroundColor = bgcolor;