質問

Hey guys I have the following:

- (void)viewDidLoad
{
     [super viewDidLoad];
        // SET BACKGROUND OF APPLICATION ON STARTUP
     [self.view setBackgroundColor: [self colorWithHexString:@"88C800"]];

     // Do any additional setup after loading the view, typically from a nib.
}

-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor grayColor];

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

    if ([cString length] != 6) return  [UIColor grayColor];

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

This will change the background color of my view controller (root) to the specified hex color I supply. My issue is, when navigating to another view controller, how do I get the above function to effect all of my view controllers? This code is within my ViewController.m

Not to sure what I need to do, suggestions and thoughts?

役に立ちましたか?

解決

I'd suggest two things:

  1. Make your colorWithHexString: a category class method on UIColor. Then you can access the method from any class that imports your category .h file.
  2. If you want every view controller in your app to have a customer color (or other common behavior), you should create a common base class such as MyBaseViewController. Have this class extend UIViewController then have all of your app's view controllers extend MyBaseViewController. Put all common setup in the base class. If you also need some table view controllers then create a MyBaseTableViewController class with similar code.

他のヒント

You can't readily do this using an instance method. You should do two things: (1) promote colorWithHexString: to be a UIColor class method (using a category); (2) if you're going to be using this same color in a lot of places, put the color where you can get at it from anywhere, such as your .pch file or some other header that everyone can import.

Here's an example from my own code:

// MyIncudes.h
@interface UIColor (mycats)
+ (UIColor*) myGolden;
+ (UIColor*) myPaler;
@end

// MyIncludes.m
@implementation UIColor (mycats)
+ (UIColor*) myGolden {
    return [self colorWithRed:1.000 green:0.894 blue:0.541 alpha:.900];
}
+ (UIColor*) myPaler {
    return [self colorWithRed:1.000 green:0.996 blue:0.901 alpha:1.000];
}
@end

Now anyone who imports MyIncludes.h has access to these colors.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top