无法在 Mac Catalyst 中以编程方式关闭警报?

Can't dismiss alerts programmatically in Mac Catalyst?

我正在显示这样的警报:

self.connectingAlert = [UIAlertController
    alertControllerWithTitle:@""
    message:NSLocalizedString(@"CONNECTING", nil)
    preferredStyle:UIAlertControllerStyleAlert];
[self.connectingAlert addAction:[UIAlertAction
    actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
    style:UIAlertActionStyleCancel
    handler:^(UIAlertAction *action) {
        [self cancelRequest];
    }]];
[self presentViewController:self.connectingAlert animated:TRUE completion:nil];

然后我想像这样以编程方式解除警报:

[self dismissViewControllerAnimated:FALSE completion:nil];

关闭代码在 iOS 中工作正常,但在 Mac Catalyst 中没有任何作用。这可能与以下事实有关:警报作为应用程序 window 的一部分呈现,有点像在应用程序之外,并且呈现样式被忽略。但我希望 dismiss 方法仍会影响 Mac 警报。

我试过这个以确保一切都正确连接:

UIViewController *test1 = self.connectingAlert.presentingViewController;
UIViewController *test2 = self.connectingAlert.presentingViewController.presentedViewController;

test1 returns 视图控制器所属的导航控制器,这看起来很奇怪,但它在 iOS 上做同样的事情。 test2 returns 我的警报。只是为了确定,我尝试了这段代码,但它也不起作用:

[self.connectingAlert.presentingViewController dismissViewControllerAnimated:FALSE completion:nil];

有没有人有这方面的经验?我在 documentation.

中没有看到任何相关信息

事实证明,虽然您应该将关闭消息发送到呈现(父)视图控制器...

[self dismissViewControllerAnimated:FALSE completion:nil];

...我必须将关闭消息发送到警报,然后它关闭:

[self.connectingAlert dismissViewControllerAnimated:FALSE completion:nil];

dismissViewControllerAnimateddocumentation 说:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

我认为这意味着 Mac Catalyst 正在对呈现和呈现的视图控制器之间的连接做一些有趣的事情。如果我检查 self.presentedViewController,就会得到 UIAlertController。但是如果我调用 self dismiss dismissViewControllerAnimated...,什么也不会发生,就好像没有呈现的视图控制器一样。但是如果我调用 self.connectingAlert dismissViewControllerAnimated...,dismiss 方法会以某种方式找到真正的呈现视图控制器。我会将此作为错误报告给 Apple。

与此同时,我很高兴有一个解决方法。