Home > Enterprise >  how do i bring a viewcontroller from the navigation controller to the front
how do i bring a viewcontroller from the navigation controller to the front

Time:11-26

Can i bring a specific ViewController from my Navigation Controller's stack to the front without reloading it or closing any of the others first? Pretty much like popToViewController but without removing the top ones

CodePudding user response:

Generally remove from viewControllers and push it again

let nav = UINavigationController(rootViewController: UIViewController())
if nav.viewControllers.count > 1 {
    let last = nav.viewControllers.removeLast()
    nav.pushViewController(last, animated: true)
}

CodePudding user response:

You can use the self.navigationController?.viewControllers to get all the navigation stack controllers and change the position as you want.

Here in the example, First I find the Controller from the navigation stack and move at last.

if let navigationController = self.navigationController,
   let indexForController = navigationController.viewControllers.firstIndex(where: {$0 is YourControllerName}) {
    let removedController = self.navigationController?.viewControllers.remove(at: indexForController)
    self.navigationController?.viewControllers.append(removedController!)
}
  • Related