ios - 从 App-delegate 重新加载或运行函数

标签 ios objective-c uitableview

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

这是来 self 的 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 重新加载上面的代码(或类似的代码)?

最佳答案

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

关于ios - 从 App-delegate 重新加载或运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18510880/

相关文章:

ios - 我可以使用网络语言(HTML - CSS - jQuery)创建 iphone 应用程序 (IOS) 吗?

objective-c - 空数组的 NSRangeException 除了有一个数组?

ios - Objective-C:在字符串中查找和替换

json - 在 Swift 中创建具有多个自定义单元格的 UITableView

ios - 如何在我的 prepareForSegue 方法中获取 cell.textlabel.text 的值?

ios - 未从自定义 uitableviewcell 调用 prepareForSegue

iOS prepareforsegue 抛出 xc_bad

ios - 如果在横向模式下 iOS 无法正常工作,则设置框架

ios - 使用 NSPredicate 的 predicateWithFormat : 时“无法解析格式字符串 "function == %@"”

ios - 按钮的图像在表格 View 中变得更长