Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to make random colors on screen when pressed the button? Objective-c

So i want to create an app that when you tap the button, the color of the screen changes. Im not sure how to do it as it should be done. Any idea?

3 Answers

You can create an array with the colors you want, and then randomly access them by index, assigning that color to the backgroundColor of the view :)

Thank you but I'm a bit confused with it . I did Crystall ball project and when the prediction array was making , when i think to create random colours , I'm getting lost.

So how should this look?

And the colour how are assigned ? in a background image ? or is there a specific way to put colours like in HTML and CSS with the hex value etc..? how and where should i create the array in h file? and the colour what value should have? like object?

There are several ways to do it.

You can check out UIColor in the documentation.

These methods may be of some use to you:

[UIColor colorWithRed: green: blue: alpha: ];
[UIColor colorWithHue: saturation: brightness: alpha: ];

There are also default color methods like:

[UIColor blackColor];
[UIColor blueColor];

You can implement the random background color like this:

//This property would be in the .m file
@property (strong, nonatomic) NSArray *colors;

//Lazily instantiating the colors array with 3 colors.
-(NSArray *)colors {
    if (!_colors) _colors = @[[UIColor blackColor],[UIColor yellowColor],[UIColor redColor]];
    return _colors;
}

//This line of code would be in an IBAction method.
self.view.backgroundColor = [self.colors objectAtIndex:randomNumber];

Hope this helps!

randomNumber should be maximum self.colors.count-1

Thank you, ill check it a bit later . Now I'm trying to do something else . bdw how long did it take you to get the basics of objC ?

Objective-C is rather easy, iOS development in general is what's more difficult.

aw , so basicaly what im learning now , its all iOS , all this file h, m this functions etc? iOS is like framework with sprites , nodles , x,y and functionality right?

Objective-C is only the language you use to develop your applications. The methodology, for example MVC, the frameworks, and all the possible things that you can build with them is what takes some time to learn.

Aa so i think i get it. Now im doing a breaking breaks game with a video online on lydna.com and i see all this stuff about gravity, speed, cordinates , placing and a lot more stuff . And as i saw in space cat (not done it , just a quick look) there were like codes h and m file for the projectile and make it what to do so i guess fire on velocity and dont lose the force and when hit disapear and the space ship exlpolde and all that .. this takes time to learn , to make the functionality about that . Its like sort of making a layout of a website and then adding to it interactivity i guess.

is Development Tools needed in app developement?

It definitely helps, it is not a requirement.

import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *colorView;

- (IBAction)changeColor:(id)sender;

@end


#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // self.colorView.frame = CGRectMake(0, 0, 320, 416);
}

- (UIColor *)randomColor {
    NSArray *colors = @[
    [UIColor redColor],
    [UIColor blueColor],
    [UIColor yellowColor],
    [UIColor greenColor],
    [UIColor purpleColor],
    [UIColor grayColor]
    ];

    int index = arc4random() % colors.count;
    return [colors objectAtIndex:index];
}

- (IBAction)changeColor:(id)sender {
    self.colorView.backgroundColor = [self randomColor];
}


@end