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

问题描述 投票:0回答:2

我使用以下代码收到此错误。我有一个名为“testViewController”的View Controller类。我正在尝试按下按钮并使其显示,但此错误显示在我的代码中。救命!

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

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

ios swift presentviewcontroller
2个回答
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在storyboard里面加载就好了

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

0
投票

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

let testViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyTest") as! testViewController // I created MyTest in the identifier field of the identity inspector.

self.present(testViewController, animated: true)
© www.soinside.com 2019 - 2024. All rights reserved.