gpt4 book ai didi

ios - 如何在 NSUserDefaults 中存储和加载 backgroundColor

转载 作者:可可西里 更新时间:2023-11-01 04:16:53 26 4
gpt4 key购买 nike

商店 -

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

加载-

- (void)viewDidLoad {   

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[colorView setBackgroundColor[UIColor [userDefaults objectForKey:@"myColor"]]];

[super viewDidLoad];

但是运行失败...也许问题是“如何从 UIView 获取背景颜色值”?

最佳答案

你可以使用

//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"]];

关于ios - 如何在 NSUserDefaults 中存储和加载 backgroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com