开发者

UIAlertView choice causing resignFirstResponder to fail

开发者 https://www.devze.com 2023-02-03 22:27 出处:网络
I\'m having a similar issue to Anthony Chan\'s question, and after trying every suggested solution, I\'m still stuck.

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!


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消