UIAlertController解雇了他的presentViewController

时间:2014-11-02 22:42:06

标签: ios objective-c uialertcontroller

我试图了解我的应用程序的奇怪行为,这是一个描述(在一个简单的项目中测试)。

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!

感谢您的帮助。

1 个答案:

答案 0 :(得分: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]];
}];