如何在没有层次结构警告的情况下在模态 VC 的父级 VC 中显示一些 VC?

How to present some VC at modalVC's parent VC without hierarchy warnings?

我尝试在弹出菜单中显示 VC 但按钮,但我有这样的层次结构警告:

Warning: Attempt to present "UIViewController: 0x14def7500" on "MyProject.MainViewController: 0x14f976400" whose view is not in the window hierarchy!

我有 MainViewController 和 PopupMenu VC 类:

Swift 4.0

class MainViewController: UIViewController, UIPopoverPresentationControllerDelegate {
        //... here is my VC code

    // showing Popup Menu VC
    @IBAction func showPopupMenu(sender: UIButton) {
        menuVC = PopupMenu()
        menuVC?.modalPresentationStyle = .popover
        menuVC?.preferredContentSize = CGSize(width: 150, height: 250)

        if let pvc = menuVC?.popoverPresentationController {
            pvc.permittedArrowDirections = .up
            pvc.delegate = self
            pvc.sourceView = sender
            pvc.sourceRect = sender.bounds
        }
        self.present(menuVC!, animated: true, completion: nil)
    }

    // showing VC from popupMenu VC
    @IBAction func showVCFromPopup(from target: PopupMenu, vc: UIViewController) {
        target.dismiss(animated: false, completion: nil) // dismiss popup

        if target.isBeingDismissed { // check is popup dismissed
            vc.modalPresentationStyle = .overCurrentContext
            self.present(vc, animated: true, completion: nil)
        }
    }


    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }

}// end of class

class PopupMenu: UIViewController {
    var button = UIButton()
    // here is init's 

    override func viewDidLoad() {
        //... some other code
        button.addTarget(self, action: #selector(vcOpen(sender:)), for: .touchUpInside)
    }

    @IBAction func vcOpen(sender: UIButton) {
        if sender == button {
            let vc = UIViewController()
            if parent != nil { print("PARENT")} // Never will work, no ideas why, so MainVC isn't a parent of PopupMenu

            if let mainVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as? MainViewController {
                print("#  ACTION: Opening VC")
                mainVC.showVCFromPopup(target: self, as: vc!) // opening VC
            }
        }
    }
}

但我有警告。 也许任何人都会在我的代码中发现错误或有任何想法如何做到这一点。

感谢大家的回答!

我编辑了您的代码以将 mainVC 的引用传递给 PopupMenu:

class MainViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    // showing Popup Menu VC
    @IBAction func showPopupMenu(sender: UIButton) {
        menuVC = PopupMenu()
        menuVC?.modalPresentationStyle = .popover
        menuVC?.preferredContentSize = CGSize(width: 150, height: 250)

        menuVC?.MainVC = self <--- here

        if let pvc = menuVC?.popoverPresentationController {
            pvc.permittedArrowDirections = .up
            pvc.delegate = self
            pvc.sourceView = sender
            pvc.sourceRect = sender.bounds
        }
        self.present(menuVC!, animated: true, completion: nil)
    }
}

class PopupMenu: UIViewController {
    var mainVC: UIViewController <-- here

    @IBAction func vcOpen(sender: UIButton) {
        if sender == button {
            mainVC.showVCFromPopup(target: self, as: vc!) <-- here
        }
    }
}