Dismiss Popover after touch

ⅰ亾dé卋堺 提交于 2019-11-30 09:15:15
Jageen

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

In your button action on the FinalViewController, have 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)
}

here is how I would do it.

I usually use lazy initialization for the PopoverViewController and it's 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 hold a reference to the UIPopoverController.

var popoverVC: UIPopoverController!

then when I show the popover i just assign the popoverController to the 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 dismiss the Popover programmatically inside an @IBAction

@IBAction func dismissPopover(sender: AnyObject) {
    popoverVC.dismissPopoverAnimated(true)
}

Inside the viewcontroller you can override viewWillAppear()

Inside of this block dimiss it

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

But the above code assumes you have a reference to the PopOver object, which I don't think is good practice based on how you described the problem.

Rather, why not have the viewcontroller that created the PopOver be responsible for destroying it. Put this in the class that listens for the button touch (which I also assumes creates the PopOver as well)

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

The popover have a View inside it and when the View it's clicked with a Tap Gesture Recognizer I show another ViewController using a modal segue.

As far as I understand from what you say, you should be able to call dismissViewControllerAnimated(_:completion:) from the action associated to your tap gesture recogniser. This will dismiss the popover you presented calling:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!