Reputation: 8629
I want to insert a new view controller just before the view controller that is currently presented on the stack. I have tried something like this:
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 )
}
However when I pop the new view controller is not showing. How would I achieve this?
Upvotes: 0
Views: 1825
Reputation: 100541
You have to re assign the array
if var vcs: [UIViewController] = navigationController?.viewControllers {
vcs.insert(vc, at: vcs.count - 1)
navigationController?.viewControllers = vcs
navigationController?.popViewController(animated: true )
}
Upvotes: 1
Reputation: 535988
You are saying this:
if var vcs: [UIViewController] = navigationController?.viewControllers {
vcs.insert(vc, at: vcs.count - 1)
But that code, alone, is pointless. You have inserted a view controller into vcs
, yes; but navigationController.viewControllers
is unaffected. And then vcs
is thrown away; it is useless.
You need to add this line:
navigationController?.viewControllers = vcs
Upvotes: 3