Keyboard will appeared automatically in ios 8.3 while displaying alertview or alertcontroller

妖精的绣舞 提交于 2019-12-03 12:27:13

Yep, it's strange.

But since iOS 8, I suggest to use the UIAlertController instead of UIAlertView.

Replace your code with this one:

- (IBAction)MethodShowAlert:(id)sender
{

    [tmptxtField resignFirstResponder];

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Check Alert textField"
                                                                              message:@"keyboard should not be open"
                                                                       preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction *action) {
                                                              [self showCustomAlertWithTitle:@"Now Check"];
                                                          }];

    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];
}


-(void)showCustomAlertWithTitle:(NSString *)title{

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title
                                                                              message:nil
                                                                       preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alertController animated:YES completion:nil];
}

The keyboard will not show after the click on the button.

In my case i tried hiding keyboard, before showing alert, so it will not save keyboard in memory to present it again after dismissing it self.

for that you just need to dismiss keyboard which will take default animation time to hide, only then you should present alert view then it will not save that keyboard.

you must put around .6 second gap in hiding keyboard and presenting alert

  [YOUR_TEXT resignFirstResponder];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{


                    _alertVw = [[UIAlertView alloc] initWithTitle:@"" message:@"message." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

                    [_alertVw show];
});

This was a change in behaviour introduced in iOS 8.3. Try downloading the iOS 8.2 simulator and you will see the old behaviour.

The result of my analysis was the following:

  1. When an alert is shown, it saves the currently showing keyboard.
  2. When an alert has completed the dismiss animation, it restores the previously saved keyboard.

So in -[id<UIAlertViewDelegate> alertView:clickedButtonAtIndex:], you are between those states. So what happens with two Alerts that are shown at the same time:

  1. Show Alert1. Save visible keyboard. Hide keyboard.
  2. User taps on alert.
  3. Show Alert2. Save that there is no keyboard.
  4. Alert1 completes dismiss animation. Restore saved keyboard. Keyboard is visible.
  5. User taps on alert.
  6. Alert2 is dismissed. Restore that there is no keyboard. Hide keyboard.

My recommendation is to use a UIAlertViewDelegate method that is called after the dismiss animation completes and show the next alert then.

Replace your alert method from below method.

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:messege title message:message 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:^{

}];

I also tried hiding the keyboard directly before presenting the alert.

What worked for me was calling [myTextField endEditing:YES]; instead of [myTextField resignFirstReponder];

This let the keyboard stay hidden, even after the alert was dismissed again.

The code looks like this:

[myTextField endEditing:YES];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"myTitle"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"NO"
                                                           style:UIAlertActionStyleCancel
                                                         handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"YES"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!