【问题标题】:How to present some VC at modalVC's parent VC without hierarchy warnings?如何在没有层次结构警告的情况下在 modalVC 的父 VC 中呈现一些 VC?
【发布时间】:2018-03-13 10:58:58
【问题描述】:

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

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

我有 MainViewController 和 PopupMenu VCs 类:

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

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

感谢所有回答!

【问题讨论】:

  • 能否提供函数openMenuVC的内容。哪一行会触发您的警告?
  • 您是否尝试在关闭完成块上显示控制器?
  • 你这样做是因为父属性为 nil 吗?所以你尝试了另一种方法?
  • 顺便说一句,父级不是呈现控制器的视图控制器。如果有的话,它就是包含它的那个。
  • @hasan83,是的,谢谢,parent 是一个 VC,其中是另一个 VC,如容器或 pageVC VC。

标签: ios swift uiviewcontroller hierarchy popover


【解决方案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
        }
    }
}

【讨论】:

  • 很好,但我不知道您是否需要担心参考循环。考虑一下。
猜你喜欢
  • 2017-07-18
  • 2013-05-04
  • 2019-05-31
  • 2021-09-11
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多