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 trialJacob Safar
890 PointsCrystal 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?
Jacob Safar
890 PointsYea. in order for the prediction to change I have to hold click and drag on the predict button.
Kai Aldag
Courses Plus Student 13,560 Pointshow 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.
Jacob Safar
890 Points- (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; }
Jacob Safar
890 Pointsdo you need more code?
Kai Aldag
Courses Plus Student 13,560 Pointsis randomPrediction a custom method?
Jacob Safar
890 PointsViewController.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
42,016 Pointsplease post the IBAction method that runs whenever your button gets pressed.
Jacob Safar
890 Points- (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; }
Kai Aldag
Courses Plus Student 13,560 PointsKai Aldag
Courses Plus Student 13,560 Pointsso what do you mean. to change the prediction you have to hold click on it?