Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard popover dismissed, delegate methods not called

I have a view controller that's presented in a popover using a storyboard segue.

enter image description here

In the presenting view controller, I had the following code:

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

However, it turns out that the presented view controller, even though it appears as a popover, has a modalPresentationStyle of '.Modal, and hence a nil popoverPresentationController. Weird!

So, I updated the code as follows:

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

The svc.popoverPresentationController delegate is now set OK, but if the popover is dismissed by the user tapping outside, none of the UIPopoverPresentationControllerDelegate delegate methods (e.g. popoverPresentationControllerShouldDismissPopover are called. What am I missing?

like image 313
Ashley Mills Avatar asked Jan 26 '16 14:01

Ashley Mills


1 Answers

No need for delegation in this case. If the presentingViewController (whatever vc is containing the popover) just overrides:

Swift 4

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    print("Dismiss: \(String(describing: self.presentedViewController))")
    super.dismiss(animated: flag, completion: completion)
}

Swift 3

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    // Before calling super get a handle on which controller is being dismissed
    print("Dismiss: \(self.presentedViewController)")
    super.dismissViewControllerAnimated(flag, completion: completion)
}

You will get notified no matter how it is dismissed. You also do not need to set any additional variables/settings in the prepareForSegue: (at least to handle this interaction).

like image 166
Firo Avatar answered Oct 14 '22 15:10

Firo