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
Kabin Wang
572 PointsStuck on "Intercepting Motion Events"
The first problem is that my debugger is empty (the debugger window thing is jut blank, nothing shows up but the app runs). The app works with button but does not respond to shake. The app runs.
This is my view controller implementation file:
// // ViewController.m // Crystalball // // Created by Kabin Wang on 2013-07-21. // Copyright (c) 2013 Kabin Wang. All rights reserved. //
import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController @synthesize predictionLabel; @synthesize predictionArray;
-
(void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"background.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.view insertSubview:imageView atIndex:0];
self.predictionArray = [[NSArray alloc] initWithObjects:@"It is decidedily so",@"hell naw",@"foshizza ma nizza",@"yuuup",nil];
// Do any additional setup after loading the view, typically from a nib. }
(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
(IBAction)buttonPressed:(UIButton *)sender { 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){ NSUInteger index= arc4random_uniform(self.predictionArray.count); self.predictionLabel.text=[self.predictionArray objectAtIndex:index]; } }
(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"motion cancelled");
} @end
3 Answers
Dave Gasparine
1,371 PointsI have my "{ NSUInteger index= arc4random_uniform(self.predictionArray.count); self.predictionLabel.text=[self.predictionArray objectAtIndex:index]; } }" in the motionBegan method and kept the NSLog for motionEnded so it displays in your log:
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if ( motion == UIEventSubtypeMotionShake){ NSLog(@"motion ended"); } }
Hope that helps :)
Kabin Wang
572 PointsHey I replaced my code with yours. Same problem :\
I can click to predict put shaking doesn't do anything and the debugger is still blank when I run.
Thanks tho
Dave Gasparine
1,371 PointsHey, I am not sure if spacing matters but looking at your original code it appears "UIEventSubtypeMotionShake" has the parenthesis without the space. Also, if you click the "Project Files" link below the video and download them, you can view the viewcontroller.m file and make sure it matches just to see where you may have gone wrong :)
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ( motion == UIEventSubtypeMotionShake ){ [self makePrediction]; } }