For those of you who work graphically, here is a diagram of the view hierarchy:
Root View -> UINavigationController -> UITableView -> Edit View -> Problem UITextfield
Basically, - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
gets called in my Root View when the device shakes.
When a user taps on the "edit" icon (a pen, in the bottom of the screen - not the traditional UINavigationBar edit button), the main view adds a subview to itself and animates it on to the screen using a custom animation.
This subview contains a UINavigationController which holds a UITableView. The UITableView, when a cell is tapped on, loads a subview into itself. This second subview is the culprit. For some reason, a UITextField in this second subview is causing problems.
When a user taps on the view, the main view will not respond to shakes unless the UITextField is active (in editing mode?).
Additional info:
My Motion Event Handler:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"%@", [event description]);
SystemSoundID SoundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"shake" ofType:@"aif"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &SoundID);
AudioServicesPlayAlertSound(SoundID);
[self genRandom:TRUE];
}
The genRandom:
Method:
/* Generate random label and apply it */
-(void)genRandom:(BOOL)deviceWasShaken{
if(deviceWasShaken == TRUE){
decisionText.text = [NSString stringWithFormat: (@"%@", [shakeReplies objectAtIndex:(arc4random() % [shakeReplies count])])];
}else{
SystemSoundID SoundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"string" ofType:@"aif"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &SoundID);
AudioServicesPlayAlertSound(SoundID);
decisionText.text = [NSString stringWithFormat: (@"%@", [pokeReplies objectAtIndex:(arc4random() % [pokeReplies count])])];
}
}
shakeReplies
and pokeReplies
are both NSArrays of strings. One is used for when a certain part of the screen is poked and one is for when the device is shaken. The app will randomly choose a string from the NSArray and display onscreen.
As always, code samples are appreciated. I've added my own to help explain the problem.