Question

I have created a custom class of UIColor so that I can easily update the color throughout my app. And I have a UITableView with various other color settings. I can't figure out how to update the custom color class to my new colors based on the selection.

Thanks for the assistance

EDIT For Clarity:

Custom Class:

+ (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];
}

I want to update the say "primaryColor" when a user taps a button....

Was it helpful?

Solution

Your code is hardcode with specific colors that can't be changed. You need to restructure the code to return variables than can be modified. Something like this:

Your .h:

@interface UIColor (MyColors)

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

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

@end

Your .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

Feel free to add the other setters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top