Could not cast value of type 'UINavigationController' to 'jacrs_ios.TopViewController'

人盡茶涼 提交于 2020-01-05 05:42:06

问题


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!

}


来源:https://stackoverflow.com/questions/45688834/could-not-cast-value-of-type-uinavigationcontroller-to-jacrs-ios-topviewcontr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!