0

在我的应用程序中,整个 UI 都是基于 tintColor 的,我让用户有机会自己选择一种颜色,然后我将其保存在NSUserDefaults

这是我的 AppDelegate 的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"])
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject: [UIColor orangeColor]];
        [defaults setObject: colorData forKey:@"ThemeColor"];
        [defaults synchronize];
        self.window.tintColor = [UIColor orangeColor];
    }
    else
    {
        NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"];
        UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
        self.window.tintColor = color;
    }  
    return YES;
}

我让用户在 UITableView 中更改颜色,那么我如何从 didSelectRowAtIndex 重新加载上面的代码(或类似的代码)?

4

1 回答 1

2
//AppDelegate.h
-(void)setUserColor;
//AppDelegate.m
-(void)setUserColor{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"])
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject: [UIColor orangeColor]];
        [defaults setObject: colorData forKey:@"ThemeColor"];
        [defaults synchronize];
        self.window.tintColor = [UIColor orangeColor];
    }
    else
    {
        NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"];
        UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
        self.window.tintColor = color;
    }  

}

然后导入 AppDelegate.h 并选择

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   [delegate setUserColor];
于 2013-08-29T12:42:31.783 回答