Search code examples
swiftios8uipopovercontroller

Dismiss Popover after touch


I've created a popover inside my MainViewController when some button its touched using the UIPopoverPresentationController and set like it's delegate like it's showed in the WWDC 2014, in the following way :

MainViewController.swift

class MainViewController : UIViewController, UIPopoverPresentationControllerDelegate {

   @IBAction func showPopover(sender: AnyObject) {

      var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("PopOverViewController") as UIViewController

      popoverContent.modalPresentationStyle = UIModalPresentationStyle.Popover
      var popover = popoverContent.popoverPresentationController

      popoverContent.preferredContentSize = CGSizeMake(250, 419)
      popover!.delegate = self
      popover!.sourceView = self.view
      popover!.sourceRect = CGRectMake(180,85,0,0)

      self.presentViewController(popoverContent, animated: true, completion: nil)        
   }  
}

The popover have a View inside it and when the View it's clicked with a Tap Gesture Recognizer I show LastViewController using a modal segue, the modal segue is created through the Interface Builder, not in code using an action to present the another LastViewController

Once the LastViewController is dismissed and I'm back in the MainViewController the popover remains open.

Inside the PopOverController I only have the default code nothing more.

LastViewController.swift

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

The above code is used to dismiss the LastViewController once the button inside is touched.

Storyboard

enter image description here

How can I dismiss the popover once the another LastViewController it's visible, or before the another LastViewController should be opened?

Thanks in advance


Solution

  • I have already answer same problem over here.
    There scenario is different but solution is same

    You have to write code for dismiss presented view controller on completion of current view controller.
    Write below code on your dismissVIew method of LastViewController.swift

     var tmpController :UIViewController! = self.presentingViewController;
    
            self.dismissViewControllerAnimated(false, completion: {()->Void in
                println("done");
                tmpController.dismissViewControllerAnimated(false, completion: nil);
            });
    


    Download link