【问题标题】:Can't dismiss alerts programmatically in Mac Catalyst?无法在 Mac Catalyst 中以编程方式关闭警报?
【发布时间】:2020-02-28 22:16:14
【问题描述】:

我正在显示这样的警报:

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

dismiss 代码在 iOS 中运行良好,但在 Mac Catalyst 中没有任何作用。这可能与警报显示为应用程序窗口的一部分(在应用程序之外)以及显示样式被忽略这一事实有关。但我希望驳回方法仍会影响 Mac 警报。

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

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

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

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

有人有这方面的经验吗?我在documentation 中没有看到任何关于它的信息。

【问题讨论】:

    标签: ios uialertcontroller mac-catalyst


    【解决方案1】:

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

    [self dismissViewControllerAnimated:FALSE completion:nil];
    

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

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

    documentationdismissViewControllerAnimated 表示:

    呈现视图控制器负责关闭视图 它呈现的控制器。如果您在呈现的视图上调用此方法 控制器本身,UIKit 要求呈现视图控制器处理 解雇。

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多