在我的应用程序中,我让用户选择背景,色调和文字的颜色。应用关闭后如何保存此设置?我的意思是当用户再次启动应用程序时,他希望看到之前使用的背景,色调和文本颜色。
这就是我设置颜色的方式。
self.BackgroundView.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];
[[[self navigationController] navigationBar] setTintColor:[UIColor colorWithRed:103.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0]];
self.QuoteText.textColor = [UIColor colorWithRed:131.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0];
尝试通过遵循Greg(可能是正确的)答案来解决,通过单击以下按钮给出SIGABRT错误:
- (IBAction)setcolor:(id)sender {
UIColor *backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:220.0/255.0 alpha:1.0];
self.BackgroundView.backgroundColor = backgroundColor;
UIColor *tintColor = [UIColor colorWithRed:129.0/255.0 green:165.0/255.0 blue:148.0/255.0 alpha:1.0];
[[[self navigationController] navigationBar] setTintColor:tintColor];
UIColor *textColor = [UIColor colorWithRed:0.0/255.0 green:98.0/255.0 blue:139.0/255.0 alpha:1.0];
self.QuoteText.textColor = textColor;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:backgroundColor forKey:@"BACKGCOLOR"];
[defaults setObject:tintColor forKey:@"TINTCOLOR"];
[defaults setObject:textColor forKey:@"TEXTCOLOR"];
[defaults synchronize];
}
答案 0 :(得分:2)
如果用户选择颜色而不是
self.BackgroundView.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];
你可以做到
UIColor *backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];
self.BackgroundView.backgroundColor = backgroundColor;
NSData *backgroundColorData = [NSKeyedArchiver archivedDataWithRootObject:backgroundColor];
UIColor *tintColor = [UIColor colorWithRed:103.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0];
[[[self navigationController] navigationBar] setTintColor:tintColor];
NSData *tintColorData = [NSKeyedArchiver archivedDataWithRootObject: tintColor];
UIColor *textColor = [UIColor colorWithRed:131.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0];
self.QuoteText.textColor = textColor;
NSData *textColorData = [NSKeyedArchiver archivedDataWithRootObject: textColor];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: backgroundColorData forKey:@"BACKGCOLOR"];
[defaults setObject:tintColorData forKey:@"TINTCOLOR"];
[defaults setObject:textColorData forKey:@"TEXTCOLOR"];
[defaults synchronize];
您对色调和文字颜色也是如此(只需使用不同的键)。
在视图控制器中,您要在viewDidLoad中更改视图颜色:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *backgColorData = [defaults objectForKey:@"BACKGCOLOR"];
UIColor *backgColor = [NSKeyedUnarchiver unarchiveObjectWithData:backgColorData];
NSData *tintColorData = [defaults objectForKey:@"TINTCOLOR"];
UIColor *tintColor = [NSKeyedUnarchiver unarchiveObjectWithData:tintColorData];
NSData *textColorData = [defaults objectForKey:@"TEXTCOLOR"];
UIColor *textColor = [NSKeyedUnarchiver unarchiveObjectWithData: textColorData];
if (backgColor)
self.BackgroundView.backgroundColor = backgColor;
if (tintColor)
[[[self navigationController] navigationBar] setTintColor:tintColor];
if (textColor)
self.QuoteText.textColor = textColor;
答案 1 :(得分:0)
Apple正在为此提供NSUserDefaults,它的行为与您的用户/应用程序的首选项相似。取自here
的示例保存:
NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:@"preferenceName"];
稍后再回来:
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];
答案 2 :(得分:0)
使用nsuserdefaults进行测试以保存设置或写入plist。