Marc Ibrahim Marc Ibrahim - 2 months ago 47
Swift Question

Not detecting Dismiss Popover programmatically

I am dismissing a popover view controller programmatically. How can i detect that in my first view controller? Is there a way to send values from the popover to the first one?
Note: popoverPresentationControllerDidDismissPopover does not work when dismissed programmatically.
Any proposition?

this is my code in the main view controller:

let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
addFriendsPopoverViewController.modalInPopover = true
addFriendsPopoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
addFriendsPopoverViewController.preferredContentSize = CGSizeMake(100, 100)
let popoverMenuViewController = addFriendsPopoverViewController.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Any
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = self.view
let height = (self.tableView.rowHeight - HeartAttributes.heartSize / 2.0 - 10) + (self.tableView.rowHeight * CGFloat((sender.view?.tag)!)) - 50
popoverMenuViewController!.sourceRect = CGRect(
x: 30,
y: height,
width: 1,
height: 1)
presentViewController(
addFriendsPopoverViewController,
animated: true,
completion: nil)


and in the popover view controller, i'm dismissing it from a button IBAction:

@IBAction func dismissPop(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)

}

Answer

The way you have worded your question is that you are looking for a function on the main view controller that is called when a popover is dismissed.

This technically happens with viewDidAppear(animated:). However, it isn't a full proof solution. If your popover doesn't cover the full screen context, this function wont fire, so it is an unreliable solution.

Really what you want is to invoke a function from the popover alerting the main view controller that it has finished/dismissed. This is easily done with a delegate protocol

protocol PopoverDelegate {
    func popoverDismissed()
}

class PopoverViewController {
    weak var delegate: PopoverDelegate?

    //Your Popover View Controller Code
}

Add the protocol conformance to your main view controller

class MainViewController: UIViewController, PopoverDelegate {
     //Main View Controller code
}

Then you need to set the delegate to for the popover to be the main view controller.

let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
addFriendsPopoverViewController.delegate = self
//The rest of your code

Finally, call this delegate function from your popover view controller when you dismiss.

@IBAction func dismissPop(sender: AnyObject) {
     dismissViewControllerAnimated(true, completion: nil)
     delegate?.popoverDismissed()
}

And in your main view controller, implement the delegate method

func popoverDismissed() {
    //Any code to run when popover is dismissed
}