连接到导航控制器时如何将ViewController弹出到根目录

时间:2019-01-31 07:08:06

标签: ios swift uiviewcontroller uinavigationcontroller popviewcontroller

我试图从连接到popToRootViewController的视图控制器调用navigationController函数。当我删除该navigationController时,无法转到我的RootViewController。但是,如果有连接到的导航控制器(我的情况:TableViewController)ViewControllerpopToRootViewController将以navigationController

结尾
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 2 && indexPath.row == 0 {
        Utilities.shared.clearTokens()
        navigationController?.popToRootViewController(animated: false)
    }
}

Storyboard

1 个答案:

答案 0 :(得分:0)

据我了解,当用户单击注销时,您想弹出到根viewController,您应该更改应用程序的根控制器,您可以这样做

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let newViewController = let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let rootViewController = storyBoard.instantiateViewController(withIdentifier:"your identifier")
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = rootViewController

因为navigationController?.popToRootViewController(animated: false)将弹出到当前导航堆栈上的根控制器