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

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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With