关闭应用程序的“最新”标签后,PopToRootViewController无法正常工作

时间:2020-01-15 22:50:51

标签: ios swift iphone

我正在处理应用程序,其注销功能无法正常工作。 here is image of storyborad

here is second image

所以我的问题是启动应用程序并注销后可以正常工作,但是当我关闭其最近的标签并再次尝试注销时,该屏幕不会返回登录屏幕。

这是我的注销代码

@IBAction func onClickLogOut(_ sender: Any)
{
    do
    {
        try Auth.auth().signOut()

        UserDefaults.standard.set(false, forKey: "isLogin")
        UserDefaults.standard.removeObject(forKey: "uid")
        UserDefaults.standard.removeObject(forKey: "email")
        UserDefaults.standard.synchronize()

    }
    catch let err
    {
        print(err.localizedDescription)
    }

    let appDel = UIApplication.shared.delegate as! AppDelegate
    let st = UIStoryboard(name: "Main", bundle: Bundle.main)
    let vc = st.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC
    let navVc = UINavigationController(rootViewController: vc!)
    appDel.window?.rootViewController = navVc
    appDel.window?.makeKeyAndVisible()

    self.parent?.navigationController?.popToRootViewController(animated: true)


}

3 个答案:

答案 0 :(得分:0)

我不确定close its recent tab是什么意思?

确定活动与您的演示页面上的注销按钮相关联吗?

我认为这里不需要调用window.makeKeyAndVisiblenavigationController.popToRootViewController。 我将编写重定向逻辑如下:

@IBAction func onClickLogOut(_ sender: Any)
{
    do
    {
        try Auth.auth().signOut()

        UserDefaults.standard.set(false, forKey: "isLogin")
        UserDefaults.standard.removeObject(forKey: "uid")
        UserDefaults.standard.removeObject(forKey: "email")
        UserDefaults.standard.synchronize()

    }
    catch let err
    {
        print(err.localizedDescription)
    }

    guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginSignUpVC") as? LoginSignUpVC else {
        print("Error: VC not found")
        return
    }

    UIApplication.shared.keyWindow?.rootViewController = vc


}

答案 1 :(得分:0)

let appDel = UIApplication.shared.delegate as! AppDelegate
    let st = UIStoryboard(name: "Main", bundle: Bundle.main)
    let vc = st.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC
    let navVc = UINavigationController(rootViewController: vc!)
    appDel.window?.rootViewController = navVc
    appDel.window?.makeKeyAndVisible()

在上面的代码中,您将创建 LoginSignUpVC 的新实例和导航控制器,并将此实例设置为根视图控制器,从而取消分配先前设置的根视图控制器。因此,不需要popToRootViewController。

答案 2 :(得分:0)

将rootviewcontroller设置为popToRootViewController后,为什么要进行LoginSignUpVC

这不是必需的,因为您已经将rootviewcontroller设置为LoginSignUpVC.

只需从代码中删除popToRootViewController行即可。

@IBAction func onClickLogOut(_ sender: Any) {

    do {
        try Auth.auth().signOut()

        UserDefaults.standard.set(false, forKey: "isLogin")
        UserDefaults.standard.removeObject(forKey: "uid")
        UserDefaults.standard.removeObject(forKey: "email")
        UserDefaults.standard.synchronize()
    }
    catch let err {
        print(err.localizedDescription)
    }

    let appDel = UIApplication.shared.delegate as! AppDelegate
    let st = UIStoryboard(name: "Main", bundle: Bundle.main)
    let vc = st.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC
    let navVc = UINavigationController(rootViewController: vc!)
    appDel.window?.rootViewController = navVc
}

我希望它对您有用!