UIAlertController rejects its presentation control

I am trying to understand the strange behavior of my application, here is the description (verified in a trivial project).

ViewControllerA introduces modally ViewControllerB

ViewControllerB contains a button, this button represents the specified UIAlertController

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

ViewControllerB presents a warning in 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, a warning will appear; if you click outside the warning, the warning will disappear (I'm on an iPad). You can do this as many times as you want ...

Here is the error. When a warning is displayed, if you press outward twice (fast enough, ~ 0.2 s interval), the warning disappears AND the ViewControllerB is rejected . At the end, we 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.

+2
ios objective-c uialertcontroller
source share
1 answer

I would rather add a UITapGestureRecognizer at the end to your UIAlertController. eg:

 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]]; }]; 
0
source share

All Articles