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.

Pixel
  • 171
  • 2
  • 10
  • 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)`. – Benjamin Lowry Mar 27 '16 at 11:44
  • 1
    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 mean – Pixel Mar 27 '16 at 14:51
  • This confused me for a bit too, but the example code at http://stackoverflow.com/a/32021691/708928 with delegates was enlightening and useful for solving this problem. – Logg Jul 10 '16 at 00:52

2 Answers2

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.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
Asim Khan
  • 508
  • 1
  • 7
  • 21
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!

David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190