Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard pops up after UIAlertView is dismissed on iOS 8.3 for iPad

With the latest iOS 8.3 release, our app starts to have a weird behavior.

After finishing textfield editing, the user can click the close button which brings up an UIAlertView. When the user clicks discard in the alertview, alertview and current view are dismissed. But somehow the keyboard shows up after the view is gone which is quite annoying to users.

After some debugging, it seems that the keyboard is shown for the last textfield that the user has accessed before closing the view. I tried various ways to endEditing for the current view in many places (before showing UIAlertView, after clicking a button in the UIAlertView; I even set the focus to another UI element of the view). It didn't solve the problem.

But for this particular issue, I'm not sure if it's a common issue or something we need to fix. Everything works perfectly before iOS 8.3.

We understand that UIAlertView is deprecated for iOS 8. We're starting to migrate to UIAlertController. But if there's any workaround, we'd love to hear.

Here's some code snippet.

- (IBAction)closeTapped:(UIButton *)sender
{
    // try to resign first responder
    // [self.tfName resignFirstResponder];
    // [self.tfPosition resignFirstResponder];
    [self.view endEditing:YES];

    if(self.orderDetails.isOpen && self.orderItemChanged)
    {
        UIAlertView* saveAlert = [[UIAlertView alloc] initWithTitle:@"Unsaved Changes"
                                                            message:@"Your changes have not been saved. Discard changes?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Save", @"Discard", nil];
        [saveAlert show];
    }
    else
    {
        [self close];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex)
    {
        case 1: // Save
        {
            [self save];
            break;
        }
        case 2: // Discard
        {
            [self close];
            break;
        }
    }
}

- (void)close
{   
    [self.delegate dismissEditOrderItemVC];
}
like image 691
Helen Avatar asked May 08 '15 17:05

Helen


4 Answers

If your deployment target is iOS 8+, try UIAlertController.

Here's a quick fix for UIAlertView: delay the invocation of showing the alert view when your text field or text view resigns first responder.

[self performSelector:@selector(showAlertView) withObject:nil afterDelay:0.6];
like image 62
Yiming Tang Avatar answered Nov 06 '22 13:11

Yiming Tang


If anyone struggles with this, I hope this will help:

if (NSClassFromString(@"UIAlertController")) {
    UIAlertController* alert = ...
}
else {
    UIAlertView* alert = ...
}
like image 44
puzzler Avatar answered Nov 06 '22 12:11

puzzler


you need to change alert for ios 8.3

first put this in your view

#define IS_IOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0

then

if (IS_IOS8) {

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Unsaved Changes" message:@"Your changes have not been saved. Discard changes?" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *saveAction = [UIAlertAction
                                    actionWithTitle:@"Save"
                                    style:UIAlertActionStyleCancel
                                    handler:^(UIAlertAction *action)
                                    {
                                        [self save];
                                    }];

        UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"Cancel"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       [alertVC dismissViewControllerAnimated:YES completion:nil];
                                   }];


        UIAlertAction *discardAction = [UIAlertAction
                                   actionWithTitle:@"Discard"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       [alertVC dismissViewControllerAnimated:YES completion:nil];
                                   }];



        [alertVC addAction:saveAction];
        [alertVC addAction:cancelAction];
        [alertVC addAction:discardAction];
        [self.view.window.rootViewController presentViewController:alertVC animated:YES completion:nil];

this will help you as it helps me in same problem. above code is compatible with both ios 7 & 8

like image 2
Nik Avatar answered Nov 06 '22 12:11

Nik


I too, had a keyboard popping up (with the cursor in the last-used textView) after closing a UIAlertController and here is a very simple fix:

Immediately before building and presenting the UIAlertController,

Using [_activeTextView resignFirstResponder]; the keyboard will reappear. Using [self.view endEditing:YES]; the keyboard will NOT reappear.

I hope this helps you.

like image 2
Steve W. Avatar answered Nov 06 '22 13:11

Steve W.