I’m sure this question is fairly simple to answer… is there any way to make variables and helpers available application-wide on the iPhone, without using the Application delegate?
Thanks!
———— edit ————
Thanks for your help. your comments helped! This is how I implemented my solution…
UIColor+Helpers.h:
#import <Foundation/Foundation.h>
@interface UIColor (Helpers)
+ (UIColor *)getHexColorWithRGB:(NSString *)rgb alpha:(float)theAlpha;
@end
UIColor+Helpers.m
#import "UIColor+Helpers.h"
@implementation UIColor (Helpers)
+ (UIColor *)getHexColorWithRGB:(NSString *)rgb alpha:(float)theAlpha {
unsigned int red, green, blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[rgb substringWithRange:range]] scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[rgb substringWithRange:range]] scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[rgb substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:theAlpha];
}
@end
One further question… would this be an accepted place to put my configuration global constants?
Lots of ways (well, two at least…)
You can use plain-old C
externs for global variablesYou can write a class method that returns a shared “global” instance (for example, the way
[NSUserDefaults standardUserDefaults]works)The standard warnings about over-use of globals apply…
Edit:
If your desired use case is to add a convenience method to supply hex values to
UIColor colorWithRed:green:blue:alpha:, a good way to do this is with an Objective-C category. For example:UIColorHelper.h contains:
and UIColorHelper.m contains the corresponding
@implementation.Once you’ve created the category on UIColor, the rest of your application can invoke it with something like:
[UIColor colorWithHexRGB:@"#3366ff"]