在显示alertview或alertcontroller时,键盘将自动出现在ios 8.3中

Vin*_*hav 9 uialertview ios8.3

我更新了Xcode 6.3和ios8.3检查我的代码.然后它给了我奇怪的结果.

这是我的演示应用程序的第一个屏幕.这是一个文本域.当我在textfield键盘打开时输入somethin.

在此输入图像描述

打字完成后.我点击了显示提醒按钮.我已经显示警报,输出将会跟随.

在此输入图像描述

点击取消后.我已经显示了另一个警报然后奇怪的结果键盘不应该打开但是当点击取消按钮时.显示另一个警报,键盘将自动出现.

这是下一个屏幕输出

在此输入图像描述

以下是代码

- (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]
  }
Run Code Online (Sandbox Code Playgroud)

Abh*_*hek 19

在我的情况下,我试图隐藏键盘,然后显示警报,所以它不会将键盘保存在内存中,以便在解除它自己后再次显示它.

为此你只需要关闭键盘,这将占用默认动画时间隐藏,然后你应该出现警报视图,然后它将不会保存该键盘.

你必须在隐藏键盘和提示警报时留出大约.6秒的差距

  [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];
});
Run Code Online (Sandbox Code Playgroud)


Lap*_*nou 15

是的,这很奇怪.

但是从iOS 8开始,我建议使用UIAlertController而不是UIAlertView.

用以下代码替换您的代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

单击按钮后键盘不会显示.


Mat*_*ats 7

这是iOS 8.3中引入的行为更改.尝试下载iOS 8.2模拟器,您将看到旧的行为.

我的分析结果如下:

  1. 显示警报时,它会保存当前显示的键盘.
  2. 当警报完成了解除动画时,它会恢复以前保存的键盘.

所以-[id<UIAlertViewDelegate> alertView:clickedButtonAtIndex:],你在这些国家之间.那么同时显示的两个警报会发生什么:

  1. 显示Alert1.保存可见键盘.隐藏键盘.
  2. 用户点击警报.
  3. 显示Alert2.保存没有键盘.
  4. Alert1完成解除动画.恢复保存的键盘.键盘可见.
  5. 用户点击警报.
  6. Alert2被驳回.恢复没有键盘.隐藏键盘.

我的建议是使用UIAlertViewDelegate在关闭动画完成后调用的方法,然后显示下一个警报.