Could not cast value of type 'UINavigationCont

2019-08-07 13:45发布

问题:

I'm new to Swift and I was trying to make a side menu base on the tutorial that I was following but I ran across with this error. "Could not cast value of type 'UINavigationController' (0x1026054a8) to 'jacrs_ios.TopViewController' (0x1009ac510)." I noticed that there were same problems already existing here but I still dont understand. I'm gonna included the current storyboard in case I did something wrong. Thanks.

ViewController Error

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let topViewController = segue.destination as! TopViewController
    topViewController.stringPassed = userName.text!
}

Storyboard

回答1:

Try this

if let navigationController = segue.destination as? UINavigationController
{

 let topViewController = navigationController?.topViewController as! TopViewController
 topViewController.stringPassed = userName.text!

}