iOS: How to define some default objects to be used anywhere?

There are many places in my app that needs to set the UIColor. I want to define the color somewhere that I can reuse it without writing the same line again, it's hard to keep track and maintain.

UIColor *myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];

I tried to make it like

#define myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];

and reuse myColor but it doesn't work. :/

Thanks!

Answers


For your define, you could write:

#define myColor [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1]

and where you use myColor, it'll be replaced outright with [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1].

Alternatively, you could write a category on UIColor that provides a method that returns your colour.

Example:

@interface UIColor (MyColors)

+ (UIColor *)myAwesomeColor;

@end

@implementation UIColor (MyColors)

+ (UIColor *)myAwesomeColor
{
    return [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
}

@end

You would then use this like [UIColor myAwesomeColor] wherever you need it, just like you do with [UIColor blackColor].


Need Your Help

Handling layout event in Vaadin

java vaadin vaadin7

I have a issue with Vaadin. It seems as the layout click event listener consumes my right clicks as well as left clicks.

Embed bold face markup in Haddock documentation

haskell haddock

Is there a way to get bold faced text when documenting Haskell source code with haddock markup? There is / / for emphasis, but nothing obvious for strong. I'm hoping for

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.