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 Build a Simple iPhone App (iOS7) Understanding Views and View Controllers CGRect and Frame

Everett Claycomb
Everett Claycomb
1,214 Points

Question about extra credit involving coloring of predictionLabel

I've been trying to figure this out for a while now, and was curious as to whether I needed to create a new reference from the label and create a UIColor object in the .h tab, and then create some sort of CGColorRef in the .m tab?

Also, it won't let me have to objects named to predictionLabel, so how could I make it both change colors and still say "YES!" when the button is pressed?

I'm probably way off, but any help would be appreciated! Thanks!

4 Answers

Everett Claycomb
Everett Claycomb
1,214 Points

I think figured it out, so for the first extra credit, you don't need to mess with the .h file at all. Within the action scope of pressing the button, the same place we made the label change its text when it was pressed, you also include: predictionLabel.textColor = [UIColor redColor];

I don't think we're supposed to create the UIColor object in the .h file, and can keep using the IBOutlet UILabel object we created earlier, but just give it a different property, which is the .textColor.

For the next extra credit, we need to make it change colors just like we made it change text. So in order to do this, I made another property just like we did for the text:

@property (strong, nonatomic) NSArray *colors;

and then I had to look up how to actually write out the format for the array, but I ended up making it look like this:

self.colors=[[NSArray alloc] initWithObjects: [UIColor redColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], [UIColor greenColor], [UIColor blackColor], [UIColor yellowColor], [UIColor magentaColor],[UIColor brownColor], [UIColor orangeColor], [UIColor lightGrayColor], nil];

(I got carried away adding colors lol) So basically it follows the exact same format as the random text, you just have to change the property you're referencing to the one you just created for colors. And then in the Action scope, you follow the same format as the text as well, but again you have to change it to reference the colors property, as well as create a new "random" variable, which i made "randomColor":

int randomColor=arc4random_uniform(self.colors.count); self.predictionLabel.textColor =[self.colors objectAtIndex:randomColor];

I hope that helps, I probably did a bad job explaining it and maybe used some wrong technical jargon, I'm still learning all of this haha. Let me know if you have questions and I'll try to explain it better.

This is really helpful. If anyone is having an issue with the random color extra credit then this is the correct solution.

Eugenio Villarreal
Eugenio Villarreal
8,041 Points

The first three lines did it, thanks! I knew I had the property correct, but couldn't figure out how to set the value to red. Cheers

Chris Atlas
Chris Atlas
2,038 Points

I'm stuck on this as well.

So far, I think we use .h tab and create:

@property (strong, nonatomic) NSArray *color

And I think we use the same format as the pseudo-random number function but use UI with predictionLabel...

Hopefully someone can help!

Eugenio Villarreal
Eugenio Villarreal
8,041 Points

So basically, just like we had to edit the text to "yes" by through the ".text" property, we change the color through the ".textColor" property. Then we set the color to red with [UIColor redColor];

this works for me ...

//don't forget to create @property first in the viewcontroller.h @property (strong, nonatomic) NSArray *colors;

//then i added self.colors

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.predictions = [[NSArray alloc] initWithObjects:@"it is Certain", @"it is decidely so", @"all signs say yes", @"the stars are not aligned", @"my reply is no", @"it is doubtful", @"better not tell you now", @"concentrate and ask again", @"unable to answer now", nil];

    self.colors = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], [UIColor greenColor], [UIColor blackColor], [UIColor yellowColor], [UIColor magentaColor],[UIColor brownColor], [UIColor orangeColor], [UIColor lightGrayColor], nil]; }

// then i made the added self self.predictionLabel.textColor

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

  • (IBAction)buttonPressed {

    int random = arc4random_uniform(self.colors.count); self.predictionLabel.textColor = [self.colors objectAtIndex:random]; }