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

Still struggling with Crystal Ball extra credit

The assignment requires that we change the color of the text randomly when the prediction button is pressed. I have gone over and over my notes and watched all the videos 3 times now. I am unable to figure out how to get the color to change randomly when the text changes. Aaron suggested I change the State Config???? This was mentioned nowhere in the videos. What is it? Here is what I have so far@implementation SDViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.predictions = [[NSArray alloc] initWithObjects: @"It is Certain", @"It is Decidedly 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", @"Maybe, yes", nil];

    self.colorsArray = [[NSArray alloc] initWithObjects: @"Eggplant", @"Turquoise", @"Lime", @"Carnation", @"Cayenne", nil];

}

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

  • (IBAction)buttonPressed { int random = arc4random_uniform(self.predictions.count); self.predictionLabel.text = [self.predictions objectAtIndex:random]; and the interface View#import <UIKit/UIKit.h>

@interface SDViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong, nonatomic) NSArray *predictions; @property (strong, nonatomic) NSArray *colorsArray;

  • (IBAction)buttonPressed; Will someone please tell me what I am missing and why??

3 Answers

Hey, I'm still very new and only just finished the Crystal Ball module myself, but I think this might help..

Firstly, I believe you set the colour via the .textColor property which only accepts a UIColor object.

  • self.predictionLabel.textColor = [UIColor brownColor];

So, naturally this would mean your array needs to be updated to be an array of UIColor objects with the colours specified. Looking at the UIColor class reference there are many other ways to define a colour, i.e. a method where you can pass RGB values or just a direct preset colour like my example above.

You would put this in the makePredicition function in the ViewController implementation class.

Yeah i am kinda in the same boat on this one....... dont know what is going wrong but i imagine im not calling the right method. here is what my code looks like but it keeps telling me objectAtIndex cannot be found in your NSArray......Not quite sure how to go about fixing it

self.colors = [[NSArray alloc] initWithObjects:@"yellowColor", @"brownColor", @"blueColor", @"purpleColor", @"redColor", nil]; }

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

  • (IBAction)buttonPressed {

    int random = arc4random_uniform(self.predictions.count); self.predictionLabel.text = [self.predictions objectAtIndex:random];

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

Exactly as Callum said, you need UIColor for the textColor setter property, but you have been feeding it NSString from your NSArray instead. A UIColor object can be defined with either the shorthand [UIColor some_color_name_from_a_limited_list] or any other methods found in the documentation.

Demetrios, in addition to the above problem, you also have an extra period character (.) after self.colors that you should remove, which is responsible for the error message you see. The compiler is probably stuck trying to make sense of self.colors.objectAtIndex. After that point, you should see a method signature argument warning before you even compile.