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

前端 未结 7 1323
小鲜肉
小鲜肉 2021-01-31 03:15

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

相关标签:
7条回答
  • 2021-01-31 03:38

    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.

    0 讨论(0)
  • 2021-01-31 03:52

    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];
    
    0 讨论(0)
  • 2021-01-31 03:52

    Try using the below code. It works fine for iOS 8 and below version

    if (IS_OS_8_OR_LATER) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction *cancelAction = [UIAlertAction
                                         actionWithTitle:@"OK"
                                         style:UIAlertActionStyleCancel
                                         handler:^(UIAlertAction *action)
                                         {
    
                                         }];
            [alertVC addAction:cancelAction];
    
            [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
    
            }];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
    

    }

    0 讨论(0)
  • 2021-01-31 03:55

    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

    0 讨论(0)
  • 2021-01-31 03:55

    I've noticed some weird behavior with textField keyboards and alertViews as well... Maybe make a bool called disableKeyboard and use it like this:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
        if (disableKeyBoard) {
    
            disableKeyboard = NO;
            return NO;
    
        } else {
    
            return YES;
    
        }
    
    }
    
    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    
        disableKeyboard = YES;
    
    }
    

    This is just a workaround and doesn't address the core issue, whatever it is. For this method to work you need to set the alertView and textField delegate methods in your header.

    0 讨论(0)
  • 2021-01-31 04:01

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

    if (NSClassFromString(@"UIAlertController")) {
        UIAlertController* alert = ...
    }
    else {
        UIAlertView* alert = ...
    }
    
    0 讨论(0)
提交回复
热议问题