6

我们在 iOS 8.3 上观察到关于键盘将显示和隐藏通知的异常行为。

viewcontroler(监听键盘通知)有一个文本文件,点击提交按钮后,该方法首先从文本字段中退出第一响应者,并显示警告以通知警告。一切正常,它会关闭键盘并按预期显示警报。(也调用 UIKeyboardWillHideNotification 方法)。

但是,在 8.3 上,在 Alertview 委托上点击 OK/Cancel 后,它会关闭警报并分别调用 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification,尽管它不应该被调用!这是出乎意料的,因为键盘在显示警报之前就已经关闭了!

这是我们正在尝试的代码片段:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    }

- (IBAction)ShowAlert:(id)sender {

    [self.TxtField resignFirstResponder];

     //This woudln't make any diff either :(
    [self.view endEditing:YES];

          [self ShowAlertForTest];

}


-(void)ShowAlertForTest{

    UIAlertView *theAlertView= [[UIAlertView alloc]initWithTitle:@"Title"

                                                         message:@"msg"

                                                        delegate:self

                                               cancelButtonTitle:@"Cancel"

                                               otherButtonTitles:@"Yes", nil];

   [theAlertView show];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     NSLog(@"buttonIndex = %ld",buttonIndex);
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSLog(@"keyboardWillShow");
}


- (void)keyboardWillHide:(NSNotification *)aNotification
{
    NSLog(@"keyboardWillHide");
}

这种行为会在我们的应用程序中引起问题,当从先前的 alertview'd 委托触发级联警报时 - 在不需要的情况下启动键盘。

非常感谢任何帮助/建议!

4

4 回答 4

2

在我们的例子中,应用程序手动隐藏了键盘(例如,当用户点击登录时,我们隐藏键盘并调用服务器登录 API)。失败后,应用程序会显示UIAlertView错误消息。当用户关闭警报时,iOS 帖子将/确实隐藏并且将/确实显示通知。当然,键盘在此序列中不会显示和隐藏,因为它已经被应用程序隐藏了。

但是,我们注意到不是手动隐藏键盘,而是让 iOS 为我们做这件事,解决了这个问题。因此,键盘在两种情况下会自动隐藏:

  1. 什么时候UIAlertView显示
  2. 当视图控制器被释放时

注意:键盘在关闭时会自动显示UIAlertView

于 2015-09-22T23:48:27.177 回答
1

我的团队通过在显示警报视图之前取消订阅键盘通知并在警报视图被关闭后重新订阅这些通知来解决问题。不理想,但它为我们解决了问题。

于 2015-06-24T21:08:45.457 回答
0

我刚刚修复了一个类似的问题。警报消失后键盘一直弹出这似乎是苹果的一个错误。我建议您使用 UIAlertController 而不是 UIAlertView。它将避免很多潜在的问题有一个简单的解决方案:如果您使用的是 UIAlertController,您可以将动画设置为 NO

[self presentViewController:alert animated:NO completion:nil];

让我知道它是否解决了您的问题

于 2016-09-29T01:08:29.990 回答
0

在我的情况下,用户点击登录按钮,然后我打电话;

[self.view endEditing: YES];
//server request here and in completion/fail alert.

键盘已关闭,alertview 很好地显示,但在取消/应用单击时,键盘再次显示并消失。但问题是有时会发生这种情况,如果服务器请求需要时间问题则看不到,如果 Alertview 立即显示问题仍然存在。所以我决定延迟通知我的警报。延迟警报解决了我的问题。希望这可以帮助。

于 2015-10-06T11:16:27.637 回答