Releasing the keyboard stops shake events. Why?

Posted by Moshe on Stack Overflow See other posts from Stack Overflow or by Moshe
Published on 2010-05-25T02:30:18Z Indexed on 2010/05/28 14:42 UTC
Read the original article Hit count: 254

Filed under:
|
|

1) How do I make a UITextField resign the keyboard and hide it? The keyboard is in a dynamically created subview whose superview looks for shake events. Resigning first responder seems to break the shake event handler.

2) how do you make the view holding the keyboard transparent, like see through glass? I have seen this done before. This part has been taken care of thanks guys.

As always, code samples are appreciated. I've added my own to help explain the problem.

EDIT:

Basically, - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event; gets called in my main view controller to handle shaking.

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.

For those of you who work graphically, here is a diagram of the view hierarchy:

Root View -> UINavigationController -> UITableView -> Edit View -> Problem UITextfield

© Stack Overflow or respective owner

Related posts about iphone

Related posts about keyboard