无法实例化视图控制器Swift

时间:2019-02-18 14:56:32

标签: swift navigation viewcontroller

到目前为止,我的代码运行良好。我看不出有什么可以改变以使其像这样的错误。当我想让用户返回首页时,它可以工作,但是在上一个ViewController重新出现在屏幕上几秒钟之后。

我尝试将“ as!HomeViewController”更改为“ as UIViewController”或“ NavigationViewController”,但它一直使用以前的ViewController。

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "home") as! HomeViewController
    self.present(balanceViewController, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:0)

  

当我想让用户返回首页...

您应该使用UINavigationController.popToViewController(_:animated:)返回到UIViewController

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.navigationController?.popToViewController(balanceViewController, animated: true)