Instead of push segue how to replace view controller (or remove from navigation stack)?

前端 未结 14 886
眼角桃花
眼角桃花 2020-11-28 01:46

I have a small iPhone app, which uses a navigation controller to display 3 views (here fullscreen):

\"Xcode

14条回答
  •  萌比男神i
    2020-11-28 02:43

    For this problem, I think the answer is just simple as

    1. Get the array of view controllers from NavigationController
    2. Removing the last ViewController (current view controller)
    3. Insert a new one at last
    4. Then set the array of ViewControllers back to the navigationController as bellow:

       if let navController = self.navigationController {
          let newVC = DestinationViewController(nibName: "DestinationViewController", bundle: nil)
      
          var stack = navController.viewControllers
          stack.remove(at: stack.count - 1)       // remove current VC
          stack.insert(newVC, at: stack.count) // add the new one
          navController.setViewControllers(stack, animated: true) // boom!
       }
      

    works perfectly with Swift 3.

    Hope it helps for some new guys.

    Cheers.

提交回复
热议问题