Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIColor函数崩溃_Iphone_Iphone Sdk 3.0_Uitableview_Crash_Uicolor - Fatal编程技术网

Iphone UIColor函数崩溃

Iphone UIColor函数崩溃,iphone,iphone-sdk-3.0,uitableview,crash,uicolor,Iphone,Iphone Sdk 3.0,Uitableview,Crash,Uicolor,好的,我有一个UITableView,我想应用主题。我想我已经知道怎么做了。我要做的是,当我的视图加载时,我调用一个名为[self-getValues]的函数。下面是它的外观: - (void)getValues { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *theme = [defaults valueForKey:@"theme"]; fileName =

好的,我有一个UITableView,我想应用主题。我想我已经知道怎么做了。我要做的是,当我的视图加载时,我调用一个名为[self-getValues]的函数。下面是它的外观:

 - (void)getValues {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *theme = [defaults valueForKey:@"theme"];
    fileName = [NSString stringWithFormat:@"%@.png", theme];
    textColor = [self getTextColor];
}
文件名和textColor变量在标题中定义为NSString和UIColor。fileName用于获取我分配给单元格的背景图像的文件名。我将textColor分配给函数[self-getTextColor]的输出。以下是该函数:

 - (UIColor*)getTextColor {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *theme = [defaults valueForKey:@"theme"];

    NSArray *themes = [NSArray arrayWithObjects:@"Crash Love", @"DECEMBERUNDERGROUND", @"Sing the Sorrow", nil];
    NSArray *textColors = [NSArray arrayWithObjects:gold, [UIColor blackColor], [UIColor whiteColor], nil];

    int r;
    UIColor *clr;

    for (NSString *element in themes) {
        r += 1;

        if ([element isEqualToString:theme]) {
            clr = [textColors objectAtIndex:r];
            break;
        }
    }

    return clr;
}
但是由于一些奇怪的原因,上面的函数崩溃了。我确信函数中存在导致崩溃的原因,因为当我删除调用函数的行时,应用程序工作正常。我已经使用了NSLog来查看我的应用程序对于主题变量得到了什么,它要么是1Theme,要么是其他,所以这不是问题所在


有什么想法吗?

换成这个怎么样:

- (UIColor*) getTextColor
{
    NSDictionary* themeColors = [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor redColor], @"Theme1",
        [UIColor blackColor], @"Theme2",
        [UIColor whiteCOlor], @"Theme3",
        nil];

    return [themeColors objectForKey: [[NSUserDefaults standardUserDefaults] valueForKey:@"theme"]];
}
颜色存储在字典中,因此您只需按名称查找即可。如果你经常调用它,那么你也可以考虑让McCior变量变为静态,只初始化一次。否则我就不麻烦了