swift - 是否可以让编译器将 VC 名称(来自字符串)视为该 VC 类型?
问题描述
我正在尝试制作一个接收视图控制器名称(字符串)的函数,然后以模态方式呈现该视图控制器。例子:
func presentViewControllerModally(newViewControllerName: String){
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "\(newViewControllerName)_Identifier") as! newViewControllerName
self.present(newViewController, animated: false, completion: nil)
}
但我不能使as! newViewControllerName
工作 好像newViewControllerName
是的类型newViewControllerName
而不是对象本身。
解决方案
您不必将控制器向下转换为您的自定义子类。你可以只是呈现UIViewController
。