3

I am having app with Slider where I have provided drop down to switch between users. As soon as app user switch to other user I want to reset entire navigation flow and start it from 1st screen.

For e.g

Screen A -> Screen B -> Screen C -> Screen D -> User opens slider and switch users -> Jump to Screen A (Remove other screen from navigation). We can consider gmail app example here, where we can switch between different accounts and gmail app redirect user to primary inbox.

EDIT :

I am using library for Slider menu available on git.

https://github.com/dekatotoro/SlideMenuControllerSwift

    class AppDelegate: UIResponder, UIApplicationDelegate {

       var window: UIWindow?
       var navigationController: UINavigationController?
       var storyboard: UIStoryboard?
       var leftViewController: LeftSidePanelViewController?
       // var uuid: String?

       func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
           if Utility.getUserStatus() == 0 {
               storyboard = UIStoryboard(name: "Main", bundle: nil)
               let mainViewController = storyboard!.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController

               leftViewController = storyboard!.instantiateViewControllerWithIdentifier("LeftSidePanelViewController") as? LeftSidePanelViewController

               navigationController = UINavigationController(rootViewController: mainViewController)
               UINavigationBar.appearance().tintColor = UIColor.whiteColor()
               leftViewController!.mainViewController = navigationController

let slideMenuController = ExSlideMenuController(mainViewController:navigationController!, leftMenuViewController: leftViewController!)
               slideMenuController.automaticallyAdjustsScrollViewInsets = true
               slideMenuController.delegate = mainViewController

self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
               self.window?.rootViewController = slideMenuController
               self.window?.makeKeyAndVisible()
               Utility.setUUID(UIDevice.currentDevice().identifierForVendor!.UUIDString)
           } else {
               storyboard = UIStoryboard(name: "Main", bundle: nil)
               let mainViewController = storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
               leftViewController = storyboard!.instantiateViewControllerWithIdentifier("LeftSidePanelViewController") as? LeftSidePanelViewController
               navigationController = UINavigationController(rootViewController: mainViewController)
               UINavigationBar.appearance().tintColor = UIColor.whiteColor()
               leftViewController!.mainViewController = navigationController

let slideMenuController = ExSlideMenuController(mainViewController:navigationController!, leftMenuViewController: leftViewController!)
               slideMenuController.automaticallyAdjustsScrollViewInsets = true
               slideMenuController.delegate = mainViewController

self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
               self.window?.rootViewController = slideMenuController
               self.window?.makeKeyAndVisible()
           }
           return true
       }
    }

I have tried different solutions for this but nothing seems to be working.

var alreadyPushed = false
           //Check if the view was already pushed
           if let viewControllers = self.navigationController?.viewControllers {
               for viewController in viewControllers {
                   if let viewController = viewController as? HomeViewController {
                       self.navigationController?.popToViewController(viewController, animated: true);
                       print(" Push Your Controller")
                       alreadyPushed = true
                       break
                   }
               }
           }

           if alreadyPushed == false {
               print("Pushing...")
               let YourControllerObject = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
               self.navigationController?.presentViewController(YourControllerObject, animated: true, completion: nil)
// HERE also tried pushViewController but that was also not working..
               self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

           }

Solution 2:-

Also tried self.navigationController?.viewController.removeAll() and then push/present the HomeView but that is also not working.

Anyone having any suggestion or tips to solve this.

2
  • This should do the job self.navigationController?.popToRootViewControllerAnimated(true) Commented Jun 6, 2016 at 11:03
  • @anishparajuli thanks for the tip but already tried this, its not working. Commented Jun 6, 2016 at 11:12

4 Answers 4

7

A very fast way:

// Put this line in the UIViewController where you want to reset navigation
self.navigationController?.viewControllers = [self]

You will erase the view controller stack and reset the navigation.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use for it

self.navigationController?.popToRootViewControllerAnimated(true)

or make some hack with navigation stack

var viewControllers = self.navigationController?.viewControllers
let firstViewCtr = viewControllers?.first;
viewControllers?.removeAll()
viewControllers?.insert(firstViewCtr!, atIndex: 0)
self.navigationController?.viewControllers = viewControllers!

1 Comment

Bad luck but both are not working for me. Not sure about the issue.
1

Instead of reverting back the navigation, Why not you can set rootViewController for Window again like below?

SharedAppDelegate.window?.rootViewController = MainStoryboard.instantiateViewControllerWithIdentifier("HomeNavigationController")

1 Comment

I have updated the code as I am using pod for slider i think there it causing issue in redirecting, because i have created demo with 4 screen and on last screen i have put a button and on button click i just wrote 1 line - navigationController.popTorootviewcontroller(true) and it worked perfectly fine. Here I think as i have used pod its causing some issue. Please check the deleagate class and let me know if you can point out something there.
1

For Swift 3, use the following code:

self?.navigationController?.popToRootViewController(animated: true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.