在iOS8.3上显示警报视图时,不必要地触发iOS键盘通知

我们正在观察关于Keyboard willshow的exception行为并将隐藏iOS 8.3上的通知。

viewcontroler(listenig to keyboard notifications)有一个textfiled,点击并点击提交按钮后,该方法首先从文本字段中重新启动第一个响应者,并显示警告以通知警告。 一切正常,它解除键盘并按预期显示警报。 (也调用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委托触发级联警报时,此行为导致我们的应用程序出现问题 – 在不需要的情况下启动键盘。

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

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

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

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

注意:解除UIAlertView时会自动显示键盘。

我的团队只是在提示警报视图之前取消订阅键盘通知并在警报视图被解除后重新订阅这些通知,从而完成了一项工作。 不理想,但它解决了我们的问题。

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

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

键盘关闭,alertview很好地显示,但在取消/应用点击键盘再次显示并消失。但问题是这有时发生,如果服务器请求需要时间问题没有看到,如果Alertview立即显示问题仍然存在。 所以我决定延迟拨打我的提醒。 把延迟放在警戒上解决了我的问题。 希望这可以帮助。

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

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

如果它解决了您的问题,请告诉我