如何重置navigationviewcontroller ios swift

问题描述 投票:0回答:4

我有带有滑块的应用程序,我提供了下拉菜单以在用户之间切换。一旦应用程序用户切换到其他用户,我想重置整个导航流程并从第一个屏幕开始。

例如

屏幕 A -> 屏幕 B -> 屏幕 C -> 屏幕 D -> 用户打开滑块并切换用户 -> 跳转到屏幕 A(从导航中删除其他屏幕)。我们可以在这里考虑 gmail 应用程序示例,我们可以在不同的帐户之间切换,并且 gmail 应用程序将用户重定向到主收件箱。

编辑:

我正在使用 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
       }
    }

我为此尝试了不同的解决方案,但似乎没有任何效果。

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)

           }

解决方案2:-

还尝试了 self.navigationController?.viewController.removeAll() ,然后推送/呈现 HomeView 但这也不起作用。

任何人有任何建议或技巧来解决这个问题。

ios uiviewcontroller uinavigationcontroller swift2
4个回答
7
投票

一个非常快速的方法:

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

您将擦除视图控制器堆栈并重置导航。


2
投票

可以用它

self.navigationController?.popToRootViewControllerAnimated(true)

或者使用导航堆栈进行一些修改

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

1
投票

与其恢复导航,为什么不可以像下面一样再次为 Window 设置 rootViewController 呢?

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

1
投票

对于 Swift 3,请使用以下代码:

self?.navigationController?.popToRootViewController(动画:true)

© www.soinside.com 2019 - 2024. All rights reserved.