UIAlertControllerclosures他的presentationViewController

我想了解我的应用程序的一个奇怪的行为,这里是一个描述(在一个微不足道的项目中testing)。

ViewControllerA呈现模态ViewControllerB

ViewControllerB包含一个button,这个button呈现一个这样指定的UIAlertController

alert = [UIAlertController alertControllerWithTitle:@"Test" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alert addAction:[UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction *handler) { NSLog(@"Action"); }]]; 

ViewControllerB以这种方式呈现警报

 - (IBAction)button:(id)sender { alert.popoverPresentationController.sourceView = self.button; alert.popoverPresentationController.sourceRect = self.button.bounds; [self presentViewController:alert animated:YES completion:nil]; } 

现在,如果您点击button,就会出现警报,如果您在警报外单击,警报消失(我在iPad上)。 你可以做很多次,只要你想…

这里是错误:当提示警报,如果你点击两次(足够快,〜0.2s的时间间隔),警报消失和ViewControllerB被解雇 。 最后我们可以看到ViewControllerA,但我们从来没有要求过。

还有一个警告信息:

 Warning: Attempt to dismiss from view controller <UIViewController: 0x7f85ab633f70> while a presentation or dismiss is in progress! 

感谢您的帮助。

我宁愿在你的UIAlertController上添加一个UITapGestureRecognizer。 喜欢 :

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Test" message:@"Test Message." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *someAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { .... }]; [alertController addAction:closeAction]; [alertController addAction:someAction]; [self presentViewController:alertController animated:YES completion:^{ [alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action:nil]]; }];