UIAlertController解除他的presentingViewController

3

我正在尝试理解我的应用程序的奇怪行为,以下是描述(在一个简单的项目中测试过)。

ViewControllerA以模态方式呈现ViewControllerB

ViewControllerB包含一个按钮,该按钮呈现了一个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];
}

现在,如果您单击按钮,会出现警报,如果您单击警报外部,警报将消失(我使用的是iPad)。您可以随意重复此操作...
以下是错误:当显示警报时,如果您在外部快速点击两次(大约0.2秒间隔),则警报会消失并且ViewControllerB也将被关闭。最后我们可以看到ViewControllerA,但我们从未要求过它。
还有一个警告消息:
Warning: Attempt to dismiss from view controller <UIViewController: 0x7f85ab633f70> while a presentation or dismiss is in progress!

谢谢您的帮助。

你能把你的简单测试项目上传到Github或类似的平台吗?我真的很想测试一下。 - matt
我在我的项目中从未调用过 dismissViewControllerAnimated:completion - Paul V
当我同时点击两个操作时,比如同时点击取消和点击外部,我的代码会出现相同的行为。你有解决方法吗? - Ido Sofi
1个回答

0
我更喜欢在您的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]];
}];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接