注销后更改rootviewcontroller

问题描述 投票:0回答:1

我已经使用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)
ios swift uinavigationcontroller rootviewcontroller
1个回答
0
投票

这不起作用,因为注销按钮操作和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将不再导航控制器,因此登录按钮操作中的pushViewController的可选链接将失败,即

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
© www.soinside.com 2019 - 2024. All rights reserved.