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!
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

Shekhar Agnihotri
349 PointsShake Event in iPhone Does not respond after the First Shake
Hello,
Following is the code I used from iPhone development learning and ran it on the simulator. Now the problem i get is that after the first event of shake the prediction is totaly blank all i see is the background image. Although I have followed all the steps as it is - can somebody be kind enough to point out the mistake. Thanks in advance
import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController @synthesize predictionLabel; @synthesize predictionArray;
-
(void)viewDidLoad { [super viewDidLoad];
UIImage *image = [UIImage imageNamed: @"dc-crystal-ball.jpg"]; UIImageView *imageView =[[UIImageView alloc] initWithImage:image]; [self. view insertSubview:imageView atIndex:0];
self.predictionArray = [[NSArray alloc] initWithObjects:@"It is decideley so", @"good things coming", @"Good time is here", @"Great profit", @"stay possitive",@"Stay Foccused",@"all is well",nil];
// Do any additional setup after loading the view, typically from a nib.
}
(void)viewDidUnload { [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [self setPredictionLabel:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. }
-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(void) makePrediction{
NSUInteger index = arc4random_uniform(self.predictionArray. count);
self.predictionLabel.text = [self.predictionArray objectAtIndex: index]; }
-(BOOL)canBecomeFirstResponder{ return YES; }
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ self.predictionLabel.text = @""; } -(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if(motion == UIEventSubtypeMotionShake){ [self predictionLabel]; } } -(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"Motion Cancelled"); } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ self.predictionLabel.text = @""; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [self predictionLabel];
}
@end
4 Answers

Pavel Palancica
6,535 PointsHey Shekhar,
Could you send me the Project source code, and I'll fix and tell you what's the problem?

Shekhar Agnihotri
349 Points@Pavel Palancia
Hello,
I have already pasted the viewcontroller.m (implementation file) in the question. Please guide me is source code something else or is the same implementation file??
Regards SA

Pavel Palancica
6,535 PointsHey man,
Be careful ;). The methods look like this:
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ( motion == UIEventSubtypeMotionShake ){ [self makePrediction]; } }
As you see, in the body of the method, you should call the "[self makePrediction];" message, not the getter of the property "[self predictionLabel];", which, basically, does nothing (visually).
Each video has a corresponding Project that you can download, you can check them first :).
Hope it helps you

Shekhar Agnihotri
349 PointsHey Pavel,
Thanks a lot. You were spot on right. I appreciate your help.
Regards Shekhar Agnihotri