如何在导航堆栈中插入新的视图控制器,然后弹出它?

时间:2018-04-15 12:49:28

标签: ios swift uinavigationcontroller

我想在当前显示在堆栈上的视图控制器之前插入一个新的视图控制器。我尝试过这样的事情:

let vc: ECAssessmentVC = ECAssessmentVC.createViewController()
vc.mode = .assessmentResults
vc.quizzes = quizzes
if var vcs: [UIViewController] = navigationController?.viewControllers {
    vcs.insert(vc, at: vcs.count - 1)
    navigationController?.popViewController(animated: true )
}

然而,当我弹出新视图控制器时没有显示。我怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

你这么说:

if var vcs: [UIViewController] = navigationController?.viewControllers {
    vcs.insert(vc, at: vcs.count - 1)

但是,仅仅这个代码是毫无意义的。您已将视图控制器插入vcs,是的;但navigationController.viewControllers不受影响。然后vcs被扔掉;它没用。

您需要添加以下行:

    navigationController?.viewControllers = vcs

答案 1 :(得分:1)

你必须重新分配数组

if var vcs: [UIViewController] = navigationController?.viewControllers {
     vcs.insert(vc, at: vcs.count - 1)
     navigationController?.viewControllers = vcs
    navigationController?.popViewController(animated: true )
}