有没有办法解雇所有视图控制器(其中一些控制器通过导航推送)并返回到根视图控制器。我看到很多例子,但它们在我的应用程序中不起作用。我正在使用swift 4
这是appdelegate中的代码
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()
}
这在我上一个View控制器中
@IBAction func okayBtnTapped(_ sender: Any) {
_ = self.navigationController?.popToRootViewController(animated:
true)
dismiss(animated: true, completion: nil)
}
您可以像这样设置一个新的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的答案
试试这个:
self.navigationController?.popToRootViewController(animated: true)
检查导航控制器,并查看该导航控制器内的控制器。看来调试问题。
这个方法popToRootViewController(动画:)是该方法应该工作
如果您正在使用简单的推送导航控制器,并且想要返回到根视图控制器,那么您只需编写
_ = self.navigationController?.popToRootViewController(animated: true)