Detect popover dismiss
Asked Answered
B

2

7

I know I can use popoverPresentationControllerDidDismissPopover but that is only called when the user taps outside the popover view to dismiss it. When I dismiss the popover manually (self.dismissViewControllerAnimated(true, completion: nil) in the popover's ViewController) nothing happens.

Boughpot answered 27/3, 2016 at 10:59 Comment(3)
You can always create a new function that contains the code you need to execute and call it in both popoverPresentationControllerDidDismissPopover and where you call self.dismissViewControllerAnimated(true, completion: nil).Kremlin
I can't because I need the code to be executed in the view controller beneath the popover and when I try to do that from the popover I get an error because its referencing an IBOutlet in the first view controller which is nil when the popover is active. I hope you can understand what I meanBoughpot
This confused me for a bit too, but the example code at https://mcmap.net/q/894050/-swift-popover-dismiss-callback with delegates was enlightening and useful for solving this problem.Recompense
E
4

Popover Dismiss!

There are two ways of detecting popover dismiss:

  1. Detecting in mainViewController, where it was actually generated, I mean ParentViewController.

Using the parentViewController as the main generating personal

class ViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate, UIPopoverPresentationControllerDelegate {

And now implementing these functions:

public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

public func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
    print("Popover dismisssed")
}
  1. Detecting in the controller used to handle popOverView made in storyboard.
func dismiss() {
    self.dismiss(animated: true, completion: nil)
    print("DISMISSS")
}
    
@IBAction func cancelClicked(_ sender: Any) {
    dismiss()
}

NOTE: For storyboards you can ask further details.

Emmer answered 3/2, 2017 at 10:43 Comment(0)
T
0

Another point is to check if you have any class inherited from your class which also implement the same method of func popoverPresentationControllerDidDismissPopover(...). If so, make sure the upper level class call the base class method like this:

 class MyINheritedClass: UIPopoverPresentationControllerDelegate {
   ...
   override func popoverPresentationControllerDidDismissPopover(
       _ popoverPresentationController: UIPopoverPresentationController)
  {
    super.popoverPresentationControllerDidDismissPopover(
        popoverPresentationController) // Make this call 
    ...
   }
}

Otherwise, your base level class method will never be called!

Tuesday answered 31/5, 2022 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.