Discard Popover after touching

I created a popover inside my MainViewController when some button touched it using the UIPopoverPresentationController and set how it delegates, as shown at WWDC 2014, as follows:

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 has a View inside it, and when the View it clicked using the Tap Gesture descriptor, I show the LastViewController using the modal segment, the modal segment is created through the Interface Builder, and not in the code, using the action to represent another LastViewController

As soon as the LastViewController deviates and I return to the MainViewController , the popover remains open.

Inside PopOverController , I no longer have the default code.

LastViewController.swift

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

The above code is used to reject the LastViewController after clicking the button inside.

Storyboard

enter image description here

How can I reject a popover after it is shown by another LastViewController , or before another LastViewController ?

Thanks in advance

+7
ios8 swift uipopovercontroller
source share
5 answers

I already answer the same problem over here .
There is a different scenario, but the solution is the same

You must write code to reject the presented view controller at the completion of the current controller.
Write the code of your rejectVIEW LastViewController.swift method LastViewController.swift

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


Download link

+17
source share

In the action of your button on FinalViewController, you tried:

 @IBAction func dismissMe() { //this should tell the popover to tell the main view controller to dismiss it. self.presentingViewController!.presentingViewController!.dismissViewControllerAnimated(false, completion: nil) } 
+5
source share

this is how i do it.

I usually use lazy initialization for PopoverViewController, and ContentViewController

 lazy var popoverVC: UIPopoverController = { let vc = UIPopoverController(contentViewController: self.contentVC) vc.delegate = self return vc }() lazy var contentVC: UIViewController = { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as UIViewController vc.modalInPopover = true return vc }() 

inside my ContentViewController I keep a link to the UIPopoverController.

 var popoverVC: UIPopoverController! 

then when i show popover i just assign popoverController to contentViewController

 @IBAction func showPopover(sender: UIButton) { contentVC.popoverVC = self.popoverVC let viewCenterRect = self.view.convertRect(self.view.bounds, toView: self.view) popoverVC.presentPopoverFromRect(CGRectMake(CGRectGetMidX(viewCenterRect), CGRectGetMidY(viewCenterRect), 1, 1), inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.allZeros, animated: true) } 

finally i reject Popover programmatically inside @IBAction

 @IBAction func dismissPopover(sender: AnyObject) { popoverVC.dismissPopoverAnimated(true) } 
+1
source share

The popover has a View inside it, and when the View clicked it using the Tap gesture Recognizer, I show another ViewController using the modal segment.

As far as I understand from what you are saying, you can call dismissViewControllerAnimated(_:completion:) from the action associated with your recognizing gesture. This will reject the invitation you submitted:

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

You can call this method on the view controller itself, depending on what is more convenient for you:

The presentation view controller is responsible for rejecting the presented presentation controller. If you call this method on the presented view controller, it automatically forwards the message to the view controller.

0
source share

Inside the viewcontroller you can override viewWillAppear ()

Inside this block dimiss it

  override public func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) _viewToDismiss.removeFromSuperView() } 

But the code above assumes that you have a reference to the PopOver object, which I believe is not good practice based on how you described the problem.

Rather, why not create the view manager that created PopOver to prevent it from being destroyed. Put this in a class that listens for the touch of a button (which I also assume also creates a PopOver)

 - (void)viewWillDisappear:(BOOL)animated { _popOver.removeFromSuperView() } 
-one
source share

All Articles