从App-delegate重新加载或运行功能

时间:2013-08-29 12:21:17

标签: ios objective-c uitableview

在我的应用程序中,整个用户界面基于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重新加载上面的代码(或类似的东西)?

1 个答案:

答案 0 :(得分: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];