1

I am making a utilities class that among other frequently used code have some methods for returning colors. However UIColor class is a part of UIKit so I wonder should I import UIKit to this subclass of NSObject, or should I return an id? Or are there other options?

Thanks in advance.

3 Answers 3

4

Instead of a subclass I use a UIColor category for custom colors

something like this:

@implementation UIColor (CustomColors)

+ (UIColor *)mb_toolBarTintColor {
    return [UIColor colorWithHue:0.5 saturation:0.1 brightness:0.3 alpha:1];
}

@end

and then I can use it with a simple

[self.toolBar setTintColor:[UIColor mb_toolBarTintColor]];
3
  • Forgot one thing, do I need to import UIKit or this category where I shall use it?
    – LuckyLuke
    Apr 15, 2011 at 18:08
  • UIKit is included in the iOS project prefix.pch anyway. So no need to import it again. Apr 15, 2011 at 19:23
  • Though I am not a big fan of categories, this seems to be a good example of perfectly reasonable usage of a category.
    – Till
    Apr 15, 2011 at 19:53
0

A subclass always needs to import the super. Make your subclass a direct subclass of UIColor, and then import the superclass, or the entire kit in the .h file.

0
0

You can also simply make a .h file that has a bunch of #define values for constants, such as:

#define TEXT_COLOR [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.