UIAlertController dismissing his presentingViewController

Deadly 提交于 2019-12-07 12:16:15

问题


I am trying to understand a weird behavior of my app, here is a description (tested in a trivial project).

ViewControllerA is presenting modally ViewControllerB

ViewControllerB contains a button, this button is presenting a UIAlertController specified this way

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

The ViewControllerB is presenting alert this way

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

Now, if you click on the button, the alert appears, if you click outside the alert, the alert disappears (I am on iPad). You can do it as many times as you want...

Here is the bug: When the alert is presented, if you click twice outside (quickly enough, ~0,2s interval), the alert disappears AND ViewControllerB is dismissed. At the end we can see ViewControllerA but we never asked for it.

There is also a warning message:

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

Thank you for your help.


回答1:


I would prefer to add a UITapGestureRecognizer at the end, on your UIAlertController. like :

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]];
}];


来源:https://stackoverflow.com/questions/26705319/uialertcontroller-dismissing-his-presentingviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!