有没有办法解雇所有视图控制器(其中一些控制器通过导航推送)并返回到根视图控制器。我看到很多例子,但它们在我的应用程序中不起作用。我正在使用swift 4
这是appdelegate中的代码
gel-long-primer-bold
这是我最后一个View控制器
func setNavigationToRootViews(){
storyBoard = UIStoryboard(name: "Main", bundle: nil)
nav = storyBoard?.instantiateViewController(withIdentifier: "mainNavigation") as! UINavigationController?
let accessToken: String? = KeychainWrapper.standard.string(forKey: "token")
print(accessToken as Any)
if accessToken != nil {
let homeVc = storyBoard?.instantiateViewController(withIdentifier: "Home-VC") as! HomeViewController
nav?.pushViewController(homeVc, animated: false)
}else{
let welcomVc = storyBoard?.instantiateViewController(withIdentifier: "login-VC") as! LoginViewController
nav?.pushViewController(welcomVc, animated: false)
}
let leftMenuVC = storyBoard?.instantiateViewController(withIdentifier: "menu-VC") as! MenuViewController
container = MFSideMenuContainerViewController.container(withCenter: nav, leftMenuViewController: leftMenuVC, rightMenuViewController: nil)
container?.panMode = MFSideMenuPanModeNone
window?.rootViewController = container
window?.makeKeyAndVisible()
}
答案 0 :(得分:4)
试试这个:
self.navigationController?.popToRootViewController(animated: true)
答案 1 :(得分:1)
您可以像这样设置一个新的RootController:
let storyboard = UIStoryboard(name: "sName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<YOUR ROOT CONTROLLER>")
self.window?.rootViewController = viewController
如果您在self.window
没有窗口,则需要根据AppDelegate实例化窗口。
如果你在NavigationController
范围内,你也可以使用@Anshul Bhatheja的答案
答案 2 :(得分:0)
检查导航控制器,并查看该导航控制器内的控制器。这似乎是调试问题。
这个方法popToRootViewController(动画:)是应该工作的方法
答案 3 :(得分:0)
如果您正在使用简单的推送导航控制器,并且想要返回到根视图控制器,那么您只需编写
_ = self.navigationController?.popToRootViewController(animated: true)
答案 4 :(得分:0)
扩展 UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
popToViewController(vc, animated: animated)
}
}
func popViewControllers(viewsToPop: Int, animated: Bool = true) {
if viewControllers.count > viewsToPop {
let vc = viewControllers[viewControllers.count - viewsToPop - 1]
popToViewController(vc, animated: animated)
}
}
}
弹出到 SomeViewController 类 navigationController?.popToViewController(ofClass: SomeViewController.self)
弹出 2 个视图控制器 navigationController?.popViewControllers(viewsToPop: 2)