tags:

views:

50

answers:

3

Hi: 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?

+2  A: 

Lots of ways (well, two at least...)

  • You can use plain-old C externs for global variables

  • You 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:

@interface UIColor (MyUIColorExtensions)
+ (UIColor *)colorWithHexRGB:(NSString *)rgb;
@end

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"]

David Gelhar
A: 

You can create .h file like myConfig.h with defines, global const variables etc and include it in prefetch file [YourProject.pch]. Also you can include there your helper categories.

Skie
A: 

You could use singleton class, so you have a single instance to contain global vats and helper functions. I'm about to detail that approach on my blog,http://blog.replicated.co.uk

Dave Meehan