简体   繁体   English

注销后更改 rootviewcontroller

[英]Change rootviewcontroller after logout

I have used SceneDelegate to change the rootviewcontroller after login action.我已经使用 SceneDelegate 在登录操作后更改 rootviewcontroller。 ?It works fine but when I logout I am not able to perform navigation again. ?它工作正常,但当我注销时,我无法再次执行导航。

Here is my code for Scenekit:这是我的 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

My logout code:我的注销代码:

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

And my login Button Action: (It doesnt works after logout action)我的登录按钮操作:(注销操作后不起作用)

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

It is not working because there is an inconsistency in hierarchy of view-controllers on logout button action and in sceneKit.它不起作用,因为注销按钮操作和sceneKit中的视图控制器层次结构不一致。 In sceneKit code you are embedding your LoginViewController in navigation controller and then assign the navigation controller as the windows root view-controller.在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

In that case you will have navigation controller in LoginViewController and the login button action works perfect ie在这种情况下,您将在LoginViewController拥有导航控制器,并且登录按钮操作完美无缺,即

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

But on logout you simply assign your LoginViewController as the windows root ie但是在注销时,您只需将您的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

After the above transition the LoginViewController will not navigation controller so the optional chaining of pushingViewController in login button action will fails ie在上述转换之后, LoginViewController将不会导航控制器,因此在登录按钮操作中 pushViewController 的可选链接将失败,即

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

Keep it consistent by embedding the LoginViewController in Navigation Controller even on logout, update your logout action code to :通过在 Navigation Controller 中嵌入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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM