我想点击Sign Out
上的ListViewController
按钮退出用户,然后返回SignInViewController
,如下图所示。
在ListViewController
为SignOut
按钮:
@IBAction func didTapSignOut(_ sender: Any) {
//Sign Out
GIDSignIn.sharedInstance().signOut()
//Go to the `SignInViewController`
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = mainStoryboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
self.navigationController?.pushViewController(desVC, animated: true)
}
当回到SignInViewController
时,有一个back
按钮,它被导回到它上面的ListViewController
。看起来应用程序仍然具有用户数据的缓存,因此用户实际上没有注销。
但我想要的是回到应用程序的初始状态,用户必须再次登录。
我如何从SignInViewController
转移到ListViewController
:在AppDelegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabBarController
self.window?.rootViewController?.present(tabbarVC, animated: false, completion: nil)
}
}
解决方案我试过:
var window: UIWindow?
@IBAction func didTapSignOut(_ sender: Any) {
GIDSignIn.sharedInstance().signOut()
let desVC: UIViewController = SignInViewController()
if let window = self.window{
window.rootViewController = desVC
}
self.navigationController?.popToRootViewController(animated: true)
}
但是现在单击按钮后视图没有改变。
在谷歌登录中,您将从signinViewcontroller呈现tabbar控制器。因此,只需在点击退出按钮时关闭标签栏控制器即可
@IBAction func didTapSignOut(_ sender: Any) {
//Sign Out
GIDSignIn.sharedInstance().signOut()
//Go to the `SignInViewController`
self.tabBarController?.dismiss(animated: true, completion: nil)
}
将self.navigationController?.pushViewController
改为self.navigationController?.popToRootViewController
(这将有一个流行动画)
如果你需要推动画,请使用self.navigationController?.setViewControllers([desVC])
更新1:
如果您不介意没有推/动画,可以直接更改窗口
let nvc = UINavigationController(rootViewController: desVC)
let window = UIApplication.shared.window
window.rootViewController = nvc
更新2:
是的,通过navigationController更新将在您的层次结构中不正确。只需更改窗口的rootViewController(如更新1中所述)