Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing Navigation Stack in Swift

I'm currently designing an application that has a funnel flow and a dashboard flow. I'd like the funnel flow to be cleared from memory on completion:

So if it goes 1) if new user start funnel -> 2) funnel screens 1-5 -> 3) funnel complete screen

I'd like to transition to dashboard screen which is not yet on the stack (it's not my head controller in this case). How can I clear the 6 screens above from memory when I transition to dashboard - basically setting a new navigation root and clearing the rest? An unwind segue doesn't appear to be able to set a new root destination.

like image 343
Eric H Avatar asked Aug 25 '15 20:08

Eric H


People also ask

How do I delete a navigation stack controller?

Use this code and enjoy: NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self. navigationController. viewControllers]; // [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.

What is navigation stack in Swift?

A navigation controller object manages its child view controllers using an ordered array, known as the navigation stack. The first view controller in the array is the root view controller and represents the bottom of the stack.


2 Answers

If you only want to clear the navigation stack, and push a new view on top of it, there is an even simpler solution.

Let's suppose you already (programmatically) assigned a navigation controller, e.g. in a viewDidLoad() function, like:

let navController = UINavigationController( rootViewController: YourRootController )
view.addSubview( navController.view )
addChildViewController( navController )

navController.didMoveToParentViewController( self )

YourRootController acts as the stacks's root (the bottom of the stack).

To push other controllers on top of the stack (your funnel controllers), simply use navController.pushViewController( yourControllerInstance!, animated: false ).

If you want to clear the stack after completion of the funnel, just call:

navController.popToRootViewControllerAnimated( false )

This function removes all views (besides the root controller) from your stack.

like image 166
Julius B. Avatar answered Sep 30 '22 03:09

Julius B.


So I ended up having to do it programmatically by popping back to the first controller and then replacing from the funnel head to the dashboard head:

func bookingCompleteAcknowledged(){
    //remove the popup controller
    dismissViewControllerAnimated(true, completion: nil)
    //remove current controller
    dismissViewControllerAnimated(true, completion: nil)

    if let topController = UIApplication.sharedApplication().keyWindow?.rootViewController {

        if let navcontroller = topController.childViewControllers[0] as? UINavigationController{
          navcontroller.popToRootViewControllerAnimated(false)

            if let funnelcontroller = navcontroller.childViewControllers[0] as? FunnelController {
                funnelcontroller.removeFromParentViewController();
                funnelcontroller.view.removeFromSuperview();

                let revealController = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! SWRevealViewController

                navcontroller.addChildViewController(revealController)
                navcontroller.view.addSubview(revealController.view)
            }
        }
    }

}
like image 30
Eric H Avatar answered Sep 30 '22 03:09

Eric H