如何在没有层次结构警告的情况下在modalVC的父VC上展示一些VC?

时间:2018-03-13 10:58:59

标签: ios swift uiviewcontroller hierarchy popover

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

  

警告:尝试在“MyProject.MainViewController:0x14f976400”上显示“UIViewController:0x14def7500”,其视图不在窗口层次结构中!

我有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
            }
        }
    }
}

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

感谢所有答案!

1 个答案:

答案 0 :(得分:1)

我编辑了代码,将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
        }
    }
}