swift2 如何重置导航视图控制器ios swift

wsxa1bj1  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(256)

我有一个应用程序与滑块,我提供了下拉菜单切换用户。只要应用程序用户切换到其他用户,我想重置整个导航流程,并从第一个屏幕开始。
例如

屏幕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)

           }

解决方案二:
还尝试了self.navigationController?.viewController.removeAll(),然后推送/呈现HomeView,但也无法正常工作。
任何人有任何建议或提示来解决这个问题。

e4yzc0pl

e4yzc0pl1#

答得极快道:

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

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

3lxsmp7m

3lxsmp7m2#

你可以为它使用

self.navigationController?.popToRootViewControllerAnimated(true)

或者用导航栈做些修改

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

ergxz8rk3#

而不是恢复导航,为什么不可以设置rootViewController窗口再次像下面这样?

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

vvppvyoh4#

对于Swift 3,请使用以下代码:
自定义导航控制器弹出到根视图控制器(动画:正确)

相关问题