【发布时间】: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