Josh O'Connor
Josh O'Connor

Reputation: 4962

How to dismiss UIPopoverPresentationController on tap inside it. (iOS, Swift)

I've been successfully able to implement a popviewcontroller, which contains a tableview of 4 cells. I want to close the popViewController when the cell is selected, but I cannot seem to do this. In my tableViewController, I have a method inside didSelectRowAtIndexPath, which calls this MasterViewController's method "updateToSelectedTab". I am able to print out the indexpath of the selected cell here, but I cannot remove the popViewController when the cell is selected. How do I do this?

enter image description here

//Here is our MasterViewController class

@IBAction func pop(sender: AnyObject) {

    let contentView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("popViewController") as! PopViewController
    contentView.modalPresentationStyle = UIModalPresentationStyle.Popover
    contentView.preferredContentSize = CGSizeMake(aButton.frame.width, 240.0)

    let popoverMenuViewController = contentView.popoverPresentationController!
    popoverMenuViewController.delegate = self
    popoverMenuViewController.permittedArrowDirections = UIPopoverArrowDirection.Any
    popoverMenuViewController.sourceView = view
    popoverMenuViewController.permittedArrowDirections = UIPopoverArrowDirection(rawValue:0)

    popoverMenuViewController.sourceRect = CGRectMake(aButton.frame.origin.x, aButton.frame.origin.y - 60, aButton.frame.width, aButton.frame.height)
    presentViewController(contentView, animated: true, completion: nil)
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.None
}

//This method is called from didSelectRowAtIndexPath of the View Controller that handles the UIPopoverPresentationController
func updateToSelectedTab(tab: Int){
    print("Current tab \(tab)")
    //Need to dismiss controller here

}

Upvotes: 0

Views: 1574

Answers (1)

Josh O'Connor
Josh O'Connor

Reputation: 4962

All I had to do was add this code in select row at index path delegate method:

dismissViewControllerAnimated(true, completion: nil)

Upvotes: 2

Related Questions