未以编程方式检测 Dismiss Popover

Not detecting Dismiss Popover programmatically

我正在以编程方式关闭弹出视图控制器。我如何在我的第一个视图控制器中检测到它?有没有办法将值从弹出窗口发送到第一个? 注意:popoverPresentationControllerDidDismissPopover 在以编程方式关闭时不起作用。 有什么提议吗?

这是我在主视图控制器中的代码:

    let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
    addFriendsPopoverViewController.modalInPopover = true
    addFriendsPopoverViewController.modalPresentationStyle =   UIModalPresentationStyle.Popover
    addFriendsPopoverViewController.preferredContentSize = CGSizeMake(100, 100)
    let popoverMenuViewController = addFriendsPopoverViewController.popoverPresentationController
    popoverMenuViewController!.permittedArrowDirections = .Any
    popoverMenuViewController!.delegate = self
    popoverMenuViewController!.sourceView = self.view
    let height = (self.tableView.rowHeight - HeartAttributes.heartSize / 2.0 - 10) + (self.tableView.rowHeight * CGFloat((sender.view?.tag)!)) - 50
    popoverMenuViewController!.sourceRect = CGRect(
        x: 30,
        y: height,
        width: 1,
        height: 1)
    presentViewController(
        addFriendsPopoverViewController,
        animated: true,
        completion: nil)   

并且在弹出视图控制器中,我通过按钮 IBAction 将其关闭:

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

}

您对问题的措辞方式是,您正在主视图控制器上寻找一个函数,该函数在弹出窗口消失时调用。

这在技术上发生在 viewDidAppear(animated:)。但是,它不是一个完整的证明解决方案。如果您的弹出窗口不覆盖全屏上下文,则此函数不会触发,因此它是一个不可靠的解决方案。

您真正想要的是从弹出窗口调用一个函数,提醒主视图控制器它具有 finished/dismissed。这很容易通过委托协议完成

protocol PopoverDelegate {
    func popoverDismissed()
}

class PopoverViewController {
    weak var delegate: PopoverDelegate?

    //Your Popover View Controller Code
}

将协议一致性添加到您的主视图控制器

class MainViewController: UIViewController, PopoverDelegate {
     //Main View Controller code
}

然后您需要将委托设置为使弹出窗口成为主视图控制器。

let addFriendsPopoverViewController = storyboard?.instantiateViewControllerWithIdentifier("HomeEmotionPopOver") as! EmotionPopOverViewController
addFriendsPopoverViewController.delegate = self
//The rest of your code

最后,当您关闭时,从弹出视图控制器调用此委托函数。

@IBAction func dismissPop(sender: AnyObject) {
     dismissViewControllerAnimated(true, completion: nil)
     delegate?.popoverDismissed()
}

然后在您的主视图控制器中,实现委托方法

func popoverDismissed() {
    //Any code to run when popover is dismissed
}

诀窍是自己关闭 segue,但让它看起来像是用户启动的,这样它就可以被委托方法检测到 popoverPresentationControllerDidDismissPopover()

我是在presentingViewControllerdismiss()函数中添加完成闭包,直接调用例程

if let pvc = self.presentingViewController {
    var didDismiss : ((UIPopoverPresentationController) -> Void)? = nil
    if let delegate = popoverPresentationController?.delegate {
        // check it is okay to dismiss the popover
        let okayToDismiss = delegate.popoverPresentationControllerShouldDismissPopover?(popoverPresentationController!) ?? true
        if okayToDismiss {
            // create completion closure
            didDismiss = delegate.popoverPresentationControllerDidDismissPopover
        }
    }
    // use local var to avoid memory leaks
    let ppc = popoverPresentationController
    // dismiss popover with completion closure
    pvc.dismiss(animated: true) {
        didDismiss?(ppc!)
    }
}

它对我来说很好用。