从 Storyboard 向下转换/向上转换视图控制器 [英] Downcast/Upcast a View Controller From Storyboard
问题描述
如果我有一个包含名为ABCViewController"的视图控制器的故事板
If I have a storyboard that contains a view controller named "ABCViewController"
A_ViewController"是ABCViewController"的子类
"A_ViewController" is a subclass of "ABCViewController"
有没有办法从情节提要中将视图控制器ABCViewController"初始化为A_ViewController"?
is there a way to initiate the view controller "ABCViewController" from the storyboard as "A_ViewController" ?
推荐答案
你不能用故事板来做到这一点.instantiateViewControllerWithIdentifier
将返回 ABCViewController
的实例,您可以将其转换为超类(即UIViewController
)但转换为子类(A_ViewController
)>) 不起作用.您可以阅读为什么不能这样做 这里.
You can't do this with storyboard. instantiateViewControllerWithIdentifier
will return instance of ABCViewController
, you can cast it to superclass (i.e.UIViewController
) but casting to subclass (A_ViewController
) won't work. You can read why you can't do this here.
如果你想拥有两个布局相同但类不同的视图控制器,你应该使用 xib
If you want to have two view controllers with same layout, but different classes you should use xib
这篇关于从 Storyboard 向下转换/向上转换视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!