如何在iOS中更新自定义类别(UIColor)?

时间:2014-02-05 01:43:53

标签: ios uicolor

我创建了一个自定义的UIColor类,以便我可以在整个应用程序中轻松更新颜色。我有一个UITableView与各种其他颜色设置。我无法弄清楚如何根据选择将自定义颜色类更新为我的新颜色。

感谢您的帮助

编辑清晰度:

自定义类:

+ (UIColor *)NPSBackgroundColor;
+ (UIColor *)NPSPrimaryColor;
+ (UIColor *)NPSSecondaryColor;
+ (UIColor *)NPSAccentColor;


+(UIColor *)NPSBackgroundColor{
return [UIColor colorWithRed: 0.909f green: 0.909f blue: 0.909f alpha:1];
}
+(UIColor *)NPSPrimaryColor{
    return [UIColor colorWithRed: 0.255 green: 0.357 blue: 0.655 alpha: 1];
}
+(UIColor *)NPSSecondaryColor{
    return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}
+(UIColor *)NPSAccentColor{
    return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
}

当用户点按按钮时,我想更新说“primaryColor”....

1 个答案:

答案 0 :(得分:2)

您的代码是具有无法更改的特定颜色的硬编码。您需要重新构造代码以返回可修改的变量。像这样:

你的.h:

@interface UIColor (MyColors)

+ (UIColor *)NPSBackgroundColor;
+ (UIColor *)NPSPrimaryColor;
+ (UIColor *)NPSSecondaryColor;
+ (UIColor *)NPSAccentColor;

+ (void)setNPPrimaryColor:(UIColor *)color;

@end

你的.m

#import "UIColor+MyColors.h"

static UIColor *NPSBackgroundColor = nil;
static UIColor *NPSPrimaryColor = nil;
static UIColor *NPSSecondaryColor = nil;
static UIColor *NPSAccentColor = nil;

@implementation UIColor (MyColors)

+(UIColor *)NPSBackgroundColor{
    if (!NPSBackgroundColor) {
        NPSBackgroundColor = [UIColor colorWithRed: 0.909f green: 0.909f blue: 0.909f alpha:1];
    }

    return NPSBackgroundColor;
}

+(UIColor *)NPSPrimaryColor{
    if (!NPSPrimaryColor) {
        return [UIColor colorWithRed: 0.255 green: 0.357 blue: 0.655 alpha: 1];
    }

    return NPSPrimaryColor;
}

+(UIColor *)NPSSecondaryColor{
    if (!NPSSecondaryColor) {
        return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
    }

    return NPSSecondaryColor;
}

+(UIColor *)NPSAccentColor{
    if (!NPSAccentColor) {
         return [UIColor colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha:1];
    }

    return NPSAccentColor;
}

+ (void)setNPSPrimaryColor:(UIColor)color {
    NPSPrimaryColor = color;
}

@end

随意添加其他制定者。