Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative Method for popoverPresentationControllerDidDismissPopover?

Tags:

ios

swift

I've just found out that popoverPresentationControllerDidDismissPopover method has been deprecated. What is an alternative method for this?

like image 408
m3-bit Avatar asked Nov 26 '19 06:11

m3-bit


2 Answers

It appears that the UIPopoverPresentationControllerDelegate protocol includes the UIAdaptivePresentationControllerDelegate protocol, which has

    // 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)

and presentationControllerDidDismiss() appears to be called when the popover is dismissed.

like image 129
Isaac Avatar answered Oct 20 '22 18:10

Isaac


Sadly, Apple's documentation leaves no hint. I would solve things this way.

You setup the popover and obtain the UIPopoverPresentationController like so:

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;

The controller object here represents the main view controller wrapped by the popover—your custom view controller. I think your best bet is to override the -viewDidDisappear: method of your custom view controller. That method will be called when the popover presentation controller dismisses the popover.

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

I think it is a shame on Apple that they provided no rationale for the deprecation or suggestions as to how to handle it. Hope that helps!

like image 27
Mario Avatar answered Oct 20 '22 19:10

Mario