【问题标题】:Alternative Method for popoverPresentationControllerDidDismissPopover?popoverPresentationControllerDidDismissPopover 的替代方法?
【发布时间】:2020-03-21 12:36:44
【问题描述】:

我刚刚发现popoverPresentationControllerDidDismissPopover 方法已被弃用。 有什么替代方法?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    看来UIPopoverPresentationControllerDelegate 协议包含UIAdaptivePresentationControllerDelegate 协议,它有

        // Called on the delegate when the user has taken action to dismiss the presentation successfully, after all animations are finished.
        // This is not called if the presentation is dismissed programatically.
        @available(iOS 13.0, *)
        optional func presentationControllerDidDismiss(_ presentationController: UIPresentationController)
    

    并且presentationControllerDidDismiss() 似乎在弹出框被解除时被调用。

    【讨论】:

    • 这应该是正确的答案。就好像你去UIPopoverPresentationControllerDelegate的定义一样。 ``` // 当用户采取行动关闭弹出框时在委托上调用。以编程方式关闭弹出框时不会调用此方法。 - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController API_DEPRECATED_WITH_REPLACEMENT("presentationControllerDidDismiss:", ios(8.0, 13.0)); ```
    【解决方案2】:

    遗憾的是,Apple 的文档没有留下任何提示。我会这样解决问题。

    您设置弹出框并获取UIPopoverPresentationController,如下所示:

    UIViewController* controller = [[MyCustomViewController alloc] init];
    controller.modalPresentationStyle = UIModalPresentationPopover;
    
    [self presentViewController:controller animated:YES completion:nil];
    
    UIPopoverPresentationController* pc = [controller popoverPresentationController];
    pc.sourceView = self.view;
    pc.sourceRect = CGRectZero;
    

    这里的controller 对象代表被弹出框包裹的主视图控制器——您的自定义视图控制器。我认为您最好的选择是覆盖自定义视图控制器的 -viewDidDisappear: 方法。当弹出框表示控制器关闭弹出框时,将调用该方法。

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
        NSLog(@"%@ - %@", NSStringFromSelector(_cmd), self);
        // Do the needful.
    }
    

    我认为 Apple 很遗憾,他们没有提供弃用理由或关于如何处理它的建议。希望对您有所帮助!

    【讨论】:

    • 这个答案并不完全正确,正确的答案是stackoverflow.com/a/60234956/721929。 UIKit UIPopoverPresentationController.h 中的 API 标头建议用presentationControllerDidDismiss 和presentationControllerShouldDismiss 替换
    • 谢谢。您链接到的将是首选解决方案。如果 Apple 可以将其放入他们的文档中,而不仅仅是头文件,那就太好了。
    【解决方案3】:

    实现UIPopoverPresentationControllerDelegate并使用popoverPresentationControllerDidDismissPopover方法。

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多