无法转换UINavigationController类型的值

时间:2018-05-23 12:01:04

标签: ios swift swift3 uinavigationcontroller

所以我有一个嵌入在导航控制器中的VC,而后者又嵌入一个包含2个容器的父VC中。对于所有三个容器,我使用函数(来自Parent VC)来操作它们来显示它们。

从图中可以看出,当我点击Voice VC中的3行按钮时,它会与10.20.30.111 | SUCCESS =>一起崩溃 如果我只是嵌入语音家长VC ..我的后退按钮不再工作Could not cast value of type 'UINavigationController' (0x1b5614200) to 'Pro.VoiceParentViewController

原因

_ = navigationController?.popToRootViewController(animated: true)

修改

po navigationController -> nil

.....

class VoiceParentViewController: UIViewController {
@IBOutlet weak var container_voiceVC: UIView!
@IBOutlet weak var container_menuVC: UIView!
@IBOutlet weak var containerSettingsVC: UIView!


@IBOutlet weak var constraint_trailingVoiceContainer: NSLayoutConstraint!
@IBOutlet weak var constraint_leadingVoiceContainer: NSLayoutConstraint!


override func viewDidLoad() {
    super.viewDidLoad()

}


func menuTouched() {
    constraint_leadingVoiceContainer.constant = self.view.bounds.maxX
    constraint_trailingVoiceContainer.constant = 2*self.view.bounds.maxX
    UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        self.view.layoutIfNeeded()
    }) { (isCompleted) in
    }
}

........

class VoiceViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


override func viewDidLoad() {
    super.viewDidLoad()



@IBAction func action_menu(_ sender: Any) {
    (self.parent as! VoiceParentViewController).menuTouched()
}

3 个答案:

答案 0 :(得分:1)

在navigationcontroller中嵌入您的ProfileViewController和VoiceViewController。

被修改

我认为问题是使用具有容器视图的子控制器。我可能错了。但我已经为您创建了一个与导航周期相同的快速演示。查看此代码 - https://drive.google.com/open?id=1faFagqDZwpfRN7lzDPknQAeWGvhx3271

enter image description here

答案 1 :(得分:0)

只需将您的VoiceParentViewController嵌入导航控制器中,而不是嵌入式VoiceViewController导航控制器中

答案 2 :(得分:0)

VoiceParentViewController中嵌入NavigationController,然后确保第一个ViewControllerVoiceParentViewController,然后推送VoiceViewController。这样可以确保navigationController?.popToRootViewController(animated: true)有效。