Swift: Popover dismiss callback

Protocols and delegations are solution to such problems. In my case I defined a protocol and conformed the MainViewController to the protocol.

//SecondViewController
protocol MyDelegate{
    func DoSomething(text:String)
}

class SecondViewController: UIViewController {

 var delegate:GetTextDelegate?

 var inputTextDelegate:String = ""

 override func viewDidLoad() {
    newText.text = inputTextDelegate
 }

 @IBAction func dismissPopover(sender: UIButton) {
        dismissViewControllerAnimated(true, completion: nil)
       //This dismisses the popover but does not notify the  MainViewConroller
 }
 @IBAction func doneButtonAction(sender: UIButton) {
    if let delegate = self.delegate {
        delegate.DoSomething(newText.text)
        self.dismissViewControllerAnimated(true, completion: nil)
    }
 }
}

class MainViewController: UIViewController, UIPopoverPresentationControllerDelegate, MyDelegate {


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {

        if segue.identifier == "GoToSecondViewControllerSegue"
        {
            var vc = segue.destinationViewController as! SecondViewController
            vc.delegate = self
            vc.inputTextDelegate = "I'm a popover!"
        }
    }

    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
        //...
    }

 func DoSomething(text: String) {
     //Do whatever you want
     println(text)
 }

}

Or, more simply, just call iOS's delegate method manually when you dismiss the popover manually.

    dismissViewControllerAnimated(true, completion: nil)
    popoverPresentationController?.delegate?.popoverPresentationControllerDidDismissPopover?(popoverPresentationController!)

You need to set yourself as the popOverDelegate. You have to do this in the popoverPresentationController of the destination.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.popoverPresentationController?.delegate = self
}

Then declare implement the delegates in your ViewController:

extension FormViewController: UIPopoverPresentationControllerDelegate {

    func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
        printBreadcrumb("Dismissed popover")
    }

}