如何在其中点击UIPopoverPresentationController。 (iOS,Swift)

时间:2016-05-27 16:38:30

标签: ios swift

我已经成功地实现了一个popviewcontroller,它包含4个单元格的tableview。我想在选择单元格时关闭popViewController,但我似乎无法做到这一点。在我的tableViewController中,我在didSelectRowAtIndexPath中有一个方法,它调用这个MasterViewController的方法“updateToSelectedTab”。我可以在这里打印出所选单元格的索引路径,但是当选择单元格时我无法删除popViewController。我该怎么做?

enter image description here

//这是我们的MasterViewController类

@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

}

1 个答案:

答案 0 :(得分:1)

我所要做的就是在索引路径委托方法的选择行中添加此代码:

dismissViewControllerAnimated(true, completion: nil)