1
votes

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)

}
2
Please post your code so your troubles can be reproduced.pedrouan
i have just edited my post, thanks for your helpMarc Ibrahim
So is closing popover from the button inside popover working?pedrouan
Yes it is. But i can't detect it from the main view controller.Marc Ibrahim
I wanted to post an answer but as I saw ColdLogic was faster, I discarded. It is exactly what you need.pedrouan

2 Answers

2
votes

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
}
1
votes

The trick is to dismiss the segue yourself but make it seem that the user initiated it so it can be detected by the delegate method popoverPresentationControllerDidDismissPopover().

I did it by adding a completion closure to the presentingViewController dismiss() function and directly invoked the routine.

if let pvc = self.presentingViewController {
    var didDismiss : ((UIPopoverPresentationController) -> Void)? = nil
    if let delegate = popoverPresentationController?.delegate {
        // check it is okay to dismiss the popover
        let okayToDismiss = delegate.popoverPresentationControllerShouldDismissPopover?(popoverPresentationController!) ?? true
        if okayToDismiss {
            // create completion closure
            didDismiss = delegate.popoverPresentationControllerDidDismissPopover
        }
    }
    // use local var to avoid memory leaks
    let ppc = popoverPresentationController
    // dismiss popover with completion closure
    pvc.dismiss(animated: true) {
        didDismiss?(ppc!)
    }
}

It is working fine for me.