[英]Can't downcast after `storyboard.instantiateViewController`
I'm using storyboard.instantiateViewController
to start a TableViewController, but when I try to downcast it, I get an error: 我正在使用storyboard.instantiateViewController
启动TableViewController,但是当我尝试向下转换时,出现错误:
Could not cast value of type 'UITableViewController' (0x109b30de8) to 'PresenceTableViewController' (0x1060329f0).
My class PresenceTableViewController is a subclass of UITableViewController, and the storyboard has assigned the VC class correctly. 我的PresenceTableViewController类是UITableViewController的子类,并且情节提要正确分配了VC类。
So why would this fail? 那为什么会失败呢?
presenceViewController = storyboard.instantiateViewController(withIdentifier: "userPresenceTable") as! PresenceTableViewController
I tried the possible cases for "Could not cast value..." and it was woking fine for all cases, and then I generated this error in one case when module was not inherited from target. 我尝试了“无法转换值...”的可能情况,并且在所有情况下都可以正常工作,然后在某个情况下,当模块未从目标继承时,我就产生了此错误。 This solved the problem for me. 这为我解决了问题。
Just re-enter the class type on storyboard, it will automatically select "Inherit module from target". 只需在情节提要上重新输入类类型,它将自动选择“从目标继承模块”。 This would solve your problem. 这样可以解决您的问题。
Also make sure that your controller from you want to use must be embed with navigation bar 还要确保您要使用的控制器必须嵌入导航栏
let tableviewController = self.storyboard?.instantiateViewController(withIdentifier: "tableviewController") as! tableviewController
self.navigationController?.pushViewController(tableviewController, animated: true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.