popoverPresentationControllerDidDismissPopover 的替代方法?

m3-*_*bit 6 ios swift

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

Isa*_*aac 12

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)
Run Code Online (Sandbox Code Playgroud)

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


Mar*_*rio 3

遗憾的是,苹果的文档没有留下任何提示。我会这样解决事情。

\n\n

您设置弹出窗口并获得UIPopoverPresentationController类似的结果:

\n\n
UIViewController* controller = [[MyCustomViewController alloc] init];\ncontroller.modalPresentationStyle = UIModalPresentationPopover;\n\n[self presentViewController:controller animated:YES completion:nil];\n\nUIPopoverPresentationController* pc = [controller popoverPresentationController];\npc.sourceView = self.view;\npc.sourceRect = CGRectZero;\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里的对象controller表示由 popover\xe2\x80\x94 自定义视图控制器包装的主视图控制器。我认为最好的选择是重写-viewDidDisappear:自定义视图控制器的方法。当弹出窗口呈现控制器关闭弹出窗口时,将调用该方法。

\n\n
- (void)viewDidDisappear:(BOOL)animated\n{\n    [super viewDidDisappear:animated];\n    NSLog(@"%@ - %@", NSStringFromSelector(_cmd), self);\n    // Do the needful.\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我认为苹果公司没有提供弃用的理由或如何处理的建议,这对苹果来说是一种耻辱。希望有帮助!

\n

  • 这个答案并不完全正确,正确的答案是/sf/answers/4216446951/。UIKit UIPopoverPresentationController.h 内的 API 标头建议用presentationControllerDidDismiss 和presentationControllerShouldDismiss 替换 (3认同)