无法将类型“ testViewController.Type”的值转换为预期的参数类型“ UIViewController”

时间:2019-04-28 13:55:27

标签: ios swift presentviewcontroller

我使用以下代码收到此错误。我有一个名为“ testViewController”的视图控制器类。我试图按一个按钮使其显示,但是此错误显示在我的代码中。救命!

self.present(testViewController, animated: true, completion: nil)

我希望这应该显示新的视图控制器,但不会。

2 个答案:

答案 0 :(得分:0)

您无需传递实例

let vc1 = TestViewController.self // wrong
let vc2 = TestViewController() // right

self.present(vc2, animated: true, completion: nil) // right
self.present(TestViewController, animated: true, completion: nil) // wrong
self.present(vc1, animated: true, completion: nil) // wrong

编辑:如果vc在情节提要板中,则加载该文件

let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") as! TestViewController
self.present(vc1, animated: true, completion: nil) // right

答案 1 :(得分:0)

我刚刚找到了有效的代码。谢谢大家在这里:

让testViewController = self.storyboard?.instantiateViewController(withIdentifier:“ MyTest”)为! testViewController // //我在身份检查器的标识符字段中创建了MyTest。

self.present(testViewController,animation:true)