Reputation: 6901
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.
Upvotes: 3
Views: 7772
Reputation: 2446
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!
Upvotes: 2
Reputation: 500
For Swift 3, use the following code:
self?.navigationController?.popToRootViewController(animated: true)
Upvotes: 1
Reputation: 3738
Instead of reverting back the navigation, Why not you can set rootViewController for Window again like below?
SharedAppDelegate.window?.rootViewController = MainStoryboard.instantiateViewControllerWithIdentifier("HomeNavigationController")
Upvotes: 1
Reputation: 2678
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.
Upvotes: 7