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

Crystal Ball iOS Project Not Changing Prediction

Hi,

When I run the simulator, and press the predict button the quote/prediction does not change. I am not sure what I am missing from my code. Well, it does change predictions, but only when I click (hold) and drag my curser on the predict button. Anyone know what my problem may be?

so what do you mean. to change the prediction you have to hold click on it?

Yea. in order for the prediction to change I have to hold click and drag on the predict button.

how long do you have to hold for? because thats very strange. if you want you can add your code so i can take a look that would be good.

  • (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; }

do you need more code?

is randomPrediction a custom method?

ViewController.h

@class SafarCrystalBall;

@interface SafarViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong, nonatomic) SafarCrystalBall *crystalBall;

  • (IBAction)buttonPressed;

@end

ViewController.m

@interface SafarViewController ()

@end

@implementation SafarViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.crystalBall = [[SafarCrystalBall alloc] init];

}

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

}

  • (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; }

This is in my "Supporting Files" CrystalBall.h

@interface SafarCrystalBall : NSObject { NSArray * _predictions; }

@property (strong, nonatomic, readonly) NSArray * predictions; -(NSString*) randomPrediction; @end

CrystalBall.m

@implementation SafarCrystalBall

  • (NSArray *) predictions { if (_predictions == nil){ _predictions = [[NSArray alloc] initWithObjects:@"It is Certain", @"It is Decidedly so", @"You Will Fail", @"My Reply is No", @"Better Not to Tell You Now",@"All Signs Say Yes", nil];

    } return _predictions; }

-(NSString*) randomPrediction{ int random = arc4random_uniform(self.predictions.count); return [self.predictions objectAtIndex:random]; }

@end

1 Answer

Stone Preston
Stone Preston
42,016 Points

please post the IBAction method that runs whenever your button gets pressed.

  • (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; }