【问题标题】:Can't cast value of type UINavigationController无法转换 UINavigationController 类型的值
【发布时间】:2018-11-02 09:59:39
【问题描述】:

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

如图所示,当我单击 Voice VC 中的 3 行按钮时,它与 Could not cast value of type 'UINavigationController' (0x1b5614200) to 'Pro.VoiceParentViewController 一起崩溃 如果我只嵌入 Voice Parent VC ..我的后退按钮不再起作用 _ = 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()
}

........

class NewConversationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var mainTableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()


@IBAction func backButtonPressed(_ sender: Any) {
    _ = navigationController?.popToRootViewController(animated: true)
}

【问题讨论】:

  • 请分享您的代码。
  • 你到底想要什么?
  • 我认为您的容器和子层次结构不清楚。请您分享您的故事板的缩小屏幕截图吗?
  • @AmritTrivedi 我已经编辑了我的问题

标签: ios swift swift3 uinavigationcontroller


【解决方案1】:

在导航控制器中嵌入了您的 ProfileViewController 和 VoiceViewController。

已编辑

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

【讨论】:

  • 仍然在同一点崩溃...在进行个人资料时...而不是在进行对话时工作
【解决方案2】:

只是将您的 VoiceParentViewController 嵌入到导航控制器中而不是在导航控制器中嵌入 VoiceViewController

【讨论】:

  • 这样做了,但现在 navigationController 为零... _ = navigationController?.popToRootViewController(animated: true) 不再有效
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-09-29
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多