我有一个 ViewController (BViewController
),它继承自另一个 UIViewController
子类 (AViewController
)。 (我想这样做的原因是我在 Storyboard中为不同的屏幕重复使用相同的 View 3 次以上。)
当我打电话时:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "AViewController") as! BViewController
self.show(vc, sender: self)
我收到这个错误:
Could not cast value of type 'test.AViewController' (0x10d08b478) to 'test.BViewController' (0x10d08b3f0).
这是我的子类,里面什么都没有。
class AViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
-
class BViewController: AViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Storyboard 中的类设置为 AViewController
因为我试图在所有子级之间共享 IBOutlets
而无需重新创建 View 。我的 UIStoryboard
中只有一个 View Controller Scene
。
最佳答案
根据此线程中的答案,不可能通过 UIStoryBoard 重用具有多个子类的单个 UIViewController 场景。然而,使用 nib 文件是可能的。
How to use single storyboard uiviewcontroller for multiple subclass
关于ios - Swift3:转换 UIViewController 子类失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40760590/