title: Run function after modal dismissed in Swift
body: <p>I want to call a function on a view controller after a modal has been dismissed. I have spent hours trying to get this to work and all of the responses I have found have not worked. I have followed instructions by others and set up a protocol but this still isn't working.</p>
  
  <p>MainController:</p>
  
  <pre><code>class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, loadStoreDelegate{
  </code></pre>
  
  <p>Then to trigger the modal I use</p>
  
  <pre><code>func displaySelectStorePopup(){
          if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView"){
              let selectStoreController = viewController
              selectStoreController.modalPresentationStyle = .Popover
              if let sctrl = selectStoreController.popoverPresentationController{
                  sctrl.delegate = self
                  sctrl.sourceView = self.view
                  sctrl.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
  
                  sctrl.permittedArrowDirections = UIPopoverArrowDirection()
                  delay(0.1){
                      sctrl.passthroughViews = nil
                  }
  
                  selectStoreController.modalInPopover = true
  
                  selectStoreController.preferredContentSize = CGSizeMake(400, 400)
  
                  self.presentViewController(selectStoreController, animated: true, completion: nil)
              }
          }
      }
  </code></pre>
  
  <p>Then the function id like to use</p>
  
  <pre><code>func loadStore() {
          print(2)
          //let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("DashboardView")
          //self.showViewController(vc as! UIViewController, sender: vc)
      }
  </code></pre>
  
  <p>ModalViewController:
  The protocol</p>
  
  <pre><code>protocol loadStoreDelegate{
      func loadStore()
  }
  
  class SelectStoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{...
  
  var delegate: loadStoreDelegate?
  </code></pre>
  
  <p>Then call the function on tableview click</p>
  
  <pre><code>func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
          self.delegate?.loadStore()
          if(tableView == selectStoreTable){
              currentStore = userStores[indexPath.row]
              self.dismissViewControllerAnimated(false, completion: nil)
          }
      }
  </code></pre>