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

梦想与她 提交于 2019-12-09 09:37:13

问题


I have updated Xcode 6.3 and ios8.3 check my code. then it gives me weird result.

here is first screen of my demo app. here is one textfield. when I type somethin in textfield keyboard open.

after typing completed. I have clicked on show alert button. I have displayed alert and output will be following.

After click on cancel. I have displayed another alert then weird result keyboard should not open but when click on cancel button. display another alert and keyboard will appear automatically.

here is next screen output

following is the code

- (IBAction)MethodShowAlert:(id)sender 
 {

[tmptxtField resignFirstResponder];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Check Alert textField" message:@"keyboard should not be open" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert show];
 }

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  {
    [self showCustomAlertWithTitle:nil];
  }


-(void)showCustomAlertWithTitle:(NSString *)title{
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Now Check" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

      [alertView show]
  }

回答1:


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.




回答2:


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];
});



回答3:


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.




回答4:


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:^{

}];



回答5:


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];


来源:https://stackoverflow.com/questions/30498972/keyboard-will-appeared-automatically-in-ios-8-3-while-displaying-alertview-or-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!