【问题标题】:Change rootviewcontroller after logout注销后更改rootviewcontroller
【发布时间】:2020-03-02 06:15:32
【问题描述】:

我在登录操作后使用 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)

【问题讨论】:

  • @Anbu.Karthik - 它和我使用的一样,但在注销后导航停止工作
  • 试试这个window?.rootViewController = UINavigationController(rootViewController: loginVC)
  • 它有效。这里有什么问题?这是正确的方法吗?
  • 是的,实际上你错过了导航控制器,因为你创建了新的根控制器

标签: ios swift uinavigationcontroller rootviewcontroller


【解决方案1】:

它不起作用,因为注销按钮操作和 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-05
    • 2021-01-28
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2015-08-29
    相关资源
    最近更新 更多