views:

174

answers:

3

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.

+1  A: 

Keyboard: Look for UIKeyboardAppearanceAlert

As for the shake detection. Once the UITextField resigns first responder (not in focus, keyboard hidden), you need to make sure another UIResponder in the chain becomes firstResponder. For example, if you have a button that sends -resignFirstResponder to the text field, you'll need to do this:

- (IBAction)pressButton {
    [textField resignFirstResponder];
    [self becomeFirstResponder];
}
Hwee-Boon Yar
This did its job and was posted first. Incomplete answer though.
Moshe
+1  A: 

try using [self.view endEditing:YES];

and for key board to be glassy use:

[textfield setKeyboardAppearance:UIKeyboardAppearanceAlert];

Hope this helps. Thanks, Madhup

Madhup
The first method is a convenience method on what I was trying. Nothing doing there, didn't work.
Moshe
+2  A: 

Does this help?

http://stackoverflow.com/questions/1342674/motionbegan-not-working

slf
Very thorough. i did change my app in the end, but this looks like the best answer here. Excellent!
Moshe