How to detect when a popover is dismissed in iOS 9
Asked Answered
I

4

13

I'm updating an app to use universal storyboards. I've created a popover segue to a new viewcontroller using interface builder by dragging from a button to my new viewcontroller and selecting 'Present As Popover' as the kind of segue.

When the user presses outside of the popover (dismissing it) I need to be notified in the presenting view controller so I can undo their actions. How can I do this?

Normally I would have created the popover manually and made my viewcontroller the popover's delegate; allowing me to use the popoverControllerDidDismissPopover delegate call back. However, this is deprecated in iOS9 and even if it wasn't I've no idea where to find the popover so I can set its delegate to my view controller.

Insurgency answered 1/11, 2015 at 23:59 Comment(0)
L
13

Not sure which method you're referring to as being deprecated but you can still use the UIPopoverPresentationControllerDelegate to achieve this. Something like:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "popoverSegue" {
        let vc = segue.destinationViewController
        sortVC.modalPresentationStyle = .Popover
        sortVC.popoverPresentationController?.sourceRect = filterButton.bounds
        sortVC.preferredContentSize = CGSizeMake(216, 150)
        sortVC.popoverPresentationController!.delegate = self
    }
}

And then use the

func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController)

method to handle its dismissal.

Luce answered 2/11, 2015 at 0:2 Comment(3)
Bear in mind that the didDismiss function only gets called when the popover dismisses itself, such as a touch outside it. If you dismiss it yourself by your own code (from a button press for example) then it won't be called.Kelcy
@Kelcy well do you know what to do then?Keratose
@Muli you could call the didDismiss function yourself whenever dismissing it yourself, if your necessary code is in the didDismiss function. Or have a function with your necessary code and call it from all places that might dismiss itKelcy
B
9

The popoverControllerDidDismissPopover: method has been replaced by popoverPresentationControllerShouldDismissPopover: because UIPopoverControllerDelegate has been replaced by the UIPopoverPresentationControllerDelegate.

From your presenting view controller, conform to the new protocol and set the delegate for the popover presentation controller in prepareForSegue::

class MyPresentingViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {         
        if let popoverPresentationController = segue.destinationViewController.popoverPresentationController {
            popoverPresentationController.delegate = self
        }
    }

    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
}

You can then use the delegate method to handle detection of the dismissal in the way that you were previously intending.

Borsch answered 2/11, 2015 at 0:51 Comment(1)
This needs to be updated because now popoverPresentationControllerShouldDismissPopover: is also deprecated.Schelling
B
2

The updated answer for this issue.

All credit to this answer:

The method you must use on iOS 13: - (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController

Barner answered 7/11, 2019 at 19:37 Comment(0)
E
1

The UIPopoverPresentationControllerDelegate inherits from UIAdaptivePresentationControllerDelegate which contains the presentationControllerShouldDismiss and presentationControllerDidDismiss as Beto points out.

I simply moved the code I had in the popover versions of these functions to UIAdaptivePresentationControllerDelegate version and they work exactly the same as before.

Did not have to change the delegate declarations on the view controller or set isModalInPresentation.

The original code still worked under 13.2.3 but those function are depreciated and one day they will stop working...or not?

my app is an iPad app using popovers and not presentation sheets or card styles.

Endless answered 1/12, 2019 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.