UIAlertView在iOS 8.3 for iPad上解除后popup键盘

随着最新的iOS 8.3版本,我们的应用程序开始有一个奇怪的行为。

完成文本字段编辑后,用户可以点击closuresbutton,调出一个UIAlertView 。 当用户在alertview中单击放弃时,alertview和当前视图将被解除。 但不知何故,键盘显示后,视图消失,这是相当烦人的用户。

经过一些debugging之后,似乎键盘显示了用户在closures视图之前访问的最后一个文本框。 我在很多地方尝试了各种方法来endEditing当前视图(在显示UIAlertView之前,单击UIAlertView一个button之后;我甚至将焦点设置到视图的另一个UI元素)。 它没有解决问题。

但是对于这个特殊的问题,我不确定这是一个普遍的问题,还是我们需要解决的问题。 一切工作在iOS 8.3之前完美。

我们知道UIAlertView在iOS 8中已被弃用。我们正在开始迁移到UIAlertController 。 但是,如果有任何解决方法,我们很乐意听到。

这是一些代码片段。

 - (IBAction)closeTapped:(UIButton *)sender { // try to resign first responder // [self.tfName resignFirstResponder]; // [self.tfPosition resignFirstResponder]; [self.view endEditing:YES]; if(self.orderDetails.isOpen && self.orderItemChanged) { UIAlertView* saveAlert = [[UIAlertView alloc] initWithTitle:@"Unsaved Changes" message:@"Your changes have not been saved. Discard changes?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", @"Discard", nil]; [saveAlert show]; } else { [self close]; } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch(buttonIndex) { case 1: // Save { [self save]; break; } case 2: // Discard { [self close]; break; } } } - (void)close { [self.delegate dismissEditOrderItemVC]; } 

如果您的部署目标是iOS 8+,请尝试使用UIAlertController

以下是对UIAlertView的快速修复:当文本字段或文本视图退出第一个响应者时,延迟显示警报视图的调用。

 [self performSelector:@selector(showAlertView) withObject:nil afterDelay:0.6]; 

如果有人为此付出了努力,我希望这会有所帮助:

 if (NSClassFromString(@"UIAlertController")) { UIAlertController* alert = ... } else { UIAlertView* alert = ... } 

您需要更改ios 8.3的警报

首先把这个在你看来

 #define IS_IOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0 

然后

 if (IS_IOS8) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Unsaved Changes" message:@"Your changes have not been saved. Discard changes?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [self save]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [alertVC dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction *discardAction = [UIAlertAction actionWithTitle:@"Discard" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [alertVC dismissViewControllerAnimated:YES completion:nil]; }]; [alertVC addAction:saveAction]; [alertVC addAction:cancelAction]; [alertVC addAction:discardAction]; [self.view.window.rootViewController presentViewController:alertVC animated:YES completion:nil]; 

这将帮助你,因为它可以帮助我解决同样的问题。 以上代码与ios 7和8兼容

尝试使用下面的代码。 它适用于iOS 8及以下版本

 if (IS_OS_8_OR_LATER) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg 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:^{ }]; } else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; } 

}

如果文本字段是第一响应者,则当警报被解除时它将自动popup键盘。 确保第一响应者被正确解雇:

 [textField resignFirstResponder] 

请记住:在表格或滚动视图中,有时该字段必须在屏幕上可见才能正确解除响应者。

如果没有第一响应者处于活动状态,则在警报解除时不应该出现键盘。

对于这个问题中的特殊情况,我build议设置一个委托方法来监听“完成”button,并在委托callback中退出第一个响应者。

或者,开始编辑时,可以存储对当前活动文本字段的引用,然后在“clickedButtonAtIndex”方法中,如果活动文本字段仍处于活动状态,则可以将其撤销。

我注意到一些奇怪的行为与textField键盘和alertViews以及…也许做一个叫做disableKeyboard的布尔,并使用它像这样:

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (disableKeyBoard) { disableKeyboard = NO; return NO; } else { return YES; } } - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { disableKeyboard = YES; } 

这只是一个解决方法,并不涉及核心问题,不pipe它是什么。 为了使这个方法起作用,你需要在你的头文件中设置alertView和textField委托方法。

我也有一个键盘在closures一个UIAlertController后popup(光标在最后使用的textView中),这是一个非常简单的修复:

在构build和呈现UIAlertController之前,

使用[_activeTextView resignFirstResponder]; 键盘将重新出现。 使用[self.view endEditing:YES]; 键盘不会再出现。

我希望这可以帮助你。