Flipkart Search

Search This Blog

Thursday, July 8, 2010

Generate Random Color

Here is a simple method to generate a random color :-
just put this method in any class you want and put the definition in the header file

in .h file
+ (UIColor *) randomColor;

in .m file
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

get random color by calling this method like this....

//Class Name is the class where you have put the method
UIColor *randColor = [ClassName randomColor];

4 comments:

Anonymous said...

When i put in the line to call the method i change the ClassName to UIColor and it tells me that there is no known class method randomNumber. What should I do?

David Douglas said...

You need to import the category to your implementation (m file)

#import "UIColor+RandomColor.h"

Anonymous said...

Thanks.. Awsomee.. (Y)

JuanManueL R. said...

I continue with the same problem. I already import what you said and xcode keeps telling me "No known class for selector RandomColor" .. RandomColor is the name of the function.. right? Is there something I could do? Thankyou!