注销后更改 rootviewcontroller

Change rootviewcontroller after logout

我在登录操作后使用 SceneDelegate 更改了 rootviewcontroller。它工作正常但是当我注销时我无法再次执行导航。

这是我的 Scenekit 代码:

 let status = UserDefaults.standard.bool(forKey: UserDefaultKeys.status)
          var rootVC : UIViewController?

          if(status == true){
            rootVC = UIStoryboard(name: AppStoryBoardName.main, bundle: nil).instantiateViewController(withIdentifier: TabBarViewController.className) as? TabBarViewController
          }else{
            rootVC = UIStoryboard(name: AppStoryBoardName.main, bundle: nil).instantiateViewController(withIdentifier: LoginViewController.className) as? LoginViewController
          }

          guard let root = rootVC else {return }
          let nav = UINavigationController(rootViewController: root)
          window?.rootViewController = nav

我的注销码:

appUserDefaults.set(false, forKey: UserDefaultKeys.status)
                                             appUserDefaults.synchronize()
            let loginVC = self.storyboard?.instantiateViewController(withIdentifier: LoginViewController.className) as? LoginViewController
            let window = UIApplication.shared.windows.first
            window?.rootViewController = loginVC

还有我的登录按钮操作:(在注销操作后不起作用)

guard let controller = self.storyboard?.instantiateViewController(withIdentifier: VerificationViewController.className) as? VerificationViewController else { return }
    controller.mobile = phoneTextField.text ?? ""
    self.navigationController?.pushViewController(controller, animated: true)

它不起作用,因为注销按钮操作和 sceneKit 中的视图控制器层次结构不一致。 在 sceneKit 代码中,您将 LoginViewController 嵌入到导航控制器中,然后将导航控制器指定为 windows 根视图控制器。

let status = UserDefaults.standard.bool(forKey: UserDefaultKeys.status)
var rootVC : UIViewController?

if(status == true){
    rootVC = UIStoryboard(name: AppStoryBoardName.main, bundle: nil).instantiateViewController(withIdentifier: TabBarViewController.className) as? TabBarViewController
} else{
    rootVC = UIStoryboard(name: AppStoryBoardName.main, bundle: nil).instantiateViewController(withIdentifier: LoginViewController.className) as? LoginViewController
}

guard let root = rootVC else {return }

// Embedding the specific controller in navigation controller and assigning navigation controller as windows root.

let nav = UINavigationController(rootViewController: root)
window?.rootViewController = nav

在那种情况下,您将在 LoginViewController 中拥有导航控制器,并且登录按钮操作完美无缺,即

self.navigationController?.pushViewController(controller, animated: true)

但是在注销时,您只需将 LoginViewController 分配为 windows 根,即

appUserDefaults.set(false, forKey: UserDefaultKeys.status) appUserDefaults.synchronize()
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: LoginViewController.className) as? LoginViewController
let window = UIApplication.shared.windows.first

// loginVC is not embedded in Navigation Controller

window?.rootViewController = loginVC

在上述转换之后,LoginViewController 将不再导航控制器,因此登录按钮操作中 pushingViewController 的可选链接将失败,即

self.navigationController?.pushViewController(controller, animated: true)

通过在导航控制器中嵌入 LoginViewController 保持一致,即使在注销时也如此,将您的注销操作代码更新为:

appUserDefaults.set(false, forKey: UserDefaultKeys.status) appUserDefaults.synchronize()
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: LoginViewController.className) as? LoginViewController
let window = UIApplication.shared.windows.first

// Embed loginVC in Navigation Controller and assign the Navigation Controller as windows root
let nav = UINavigationController(rootViewController: loginVC)
window?.rootViewController = nav