2

I'm having a similar issue to Anthony Chan's question, and after trying every suggested solution, I'm still stuck.

Somehow, only after interacting with my UIAlertView, I'm unable to dismiss the keyboard in another view of my app. It's as though the Alert is breaking my UITextField's ability to resignFirstResponder. Below I instantiate my UIAlertView, which then calls its didDismissWIthButtonIndex method. Then, I call the showInfo method, which loads another UIViewController.

UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                            message:@"error message text."
                           delegate:self
                  cancelButtonTitle:@"Not now"
                  otherButtonTitles:@"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];

Once the 'Settings' option is pressed, I'm calling this method:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {   
    if ([alertView tag] == 2) {    
        if (buttonIndex == 1){      
            [self showInfo:nil];
        }
    }   
}

My showInfo method loads the other ViewController, via the code below:

- (IBAction)showInfo:(id)sender {
    FlipsideViewController *fscontroller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    fscontroller.delegate = self;
    fscontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:fscontroller animated:YES];
    [fscontroller release];
}

Upon clicking any textField in this Flipside VC, I'm unable to dismiss the keyboard as I normally can with - (BOOL)textFieldShouldReturn:(UITextField *)textField, and [textField resignFirstResponder]. I've omitted this code bc this question is getting long, but I'm happy to post if necessary.

The interesting part is that if I comment out the [self showInfo:nil] call made when the button is clicked and call it by clicking a test button (outside the alertView didDismissWithButtonIndex: method), everything works fine. Any idea what's happening here?

Thanks in advance!

Community
  • 1
  • 1
Chazbot
  • 1,890
  • 1
  • 13
  • 23
  • 1
    Update: Changing [self showInfo:nil] to [self performSelector:@selector(showInfo:) withObject:nil afterDelay:.1] works, but it seems like that might break on slower devices (or outside the simulator)... it feels like a strange scope issue. Any help is appreciated! – Chazbot Jan 13 '11 at 22:10
  • Ran into the same issue that you describe. For some reason if you schedule the modal presentation (or dismiss, in my case) to run on the next run loop then the problem goes away - the keyboard doesn't get stuck as the first responder. Thanks! – tyler Sep 07 '12 at 22:11
  • I should also note - this issue seems to only affect iOS4. I do not see this issue in iOS5+. – tyler Sep 07 '12 at 22:15

2 Answers2

1

When an alert, with more than one dismissal option, is called above a keyboard - the keyboard becomes un-dismissible with resignFirstResponder on the active textfield;

You will need to dismiss the keyboard before showing the alert.

Assuming your UITextField is called myTextField;

[myTextField resignFirstResponder]; //That's the only line I added

UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                         message:@"error message text."
                                                        delegate:self
                                               cancelButtonTitle:@"Not now"
                                               otherButtonTitles:@"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];

I hope this helps anyone who had to deal with this oddly obscure issue.

technophobia
  • 2,644
  • 1
  • 20
  • 29
-1

You should not call alertView:didDismissWithButtonIndex: directly. This delegate method will be executed automatically in all cases after the alert has disappeared. Otherwise the code will be run twice!

Felix
  • 35,354
  • 13
  • 96
  • 143
  • Thanks, but I'm not calling it directly; It's being called when the user taps on one of the buttons in the UIAlertView. – Chazbot Jan 13 '11 at 22:37