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

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

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

\"Xcode

14条回答
  •  [愿得一人]
    2020-11-28 02:50

    the swift 2 version of ima747 answer:

    override func perform() {
        let navigationController: UINavigationController = sourceViewController.navigationController!;
    
        var controllerStack = navigationController.viewControllers;
        let index = controllerStack.indexOf(sourceViewController);
        controllerStack[index!] = destinationViewController
    
        navigationController.setViewControllers(controllerStack, animated: true);
    }
    

    As he mentioned it has the following advantages:

    • Can work anywhere in the view stack, not just the top view (not sure if this is realistically ever needed or even technically possible to trigger, but hey it's in there).
    • It doesn't cause a pop OR transition to the previous view controller before displaying the replacement, it just displays the new controller with a natural transition, with the back navigation being to the same back navigation of the source controller.

提交回复
热议问题