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

前端 未结 14 775
眼角桃花
眼角桃花 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.
    0 讨论(0)
  • 2020-11-28 02:50

    Using unwind segue would be the most appropriate solution to this problem. I agree with Lauro.

    Here is a brief explanation to setup an unwind segue from detailsViewController[or viewController3] to myAuthViewController[or viewController1]

    This is essentially how you would go about performing an unwind segue through the code.

    • Implement an IBAction method in the viewController you want to unwind to(in this case viewController1). The method name can be anything so long that it takes one argument of the type UIStoryboardSegue.

      @IBAction func unwindToMyAuth(segue: UIStoryboardSegue) {
      println("segue with ID: %@", segue.Identifier)
      }
      
    • Link this method in the viewController(3) you want to unwind from. To link, right click(double finger tap) on the exit icon at the top of the viewController, at this point 'unwindToMyAuth' method will show in the pop up box. Control click from this method to the first icon, the viewController icon(also present at the top of the viewController, in the same row as the exit icon). Select the 'manual' option that pops up.

    • In the Document outline, for the same view(viewController3), select the unwind segue you just created. Go to the attributed inspector and assign a unique identifier for this unwind segue. We now have a generic unwind segue ready to be used.

    • Now, the unwind segue can be performed just like any other segue from the code.

      performSegueWithIdentifier("unwind.to.myauth", sender: nil)
      

    This approach, will take you from viewController3 to viewController1 without the need to remove viewController2 from the navigation hierarchy.

    Unlike other segues, unwind segues do not instantiate a view controller, they only go to an existing view controller in the navigation hierarchy.

    0 讨论(0)
提交回复
热议问题