首页 > 解决方案 > 成功登录后无法关闭 UIViewController

问题描述

登录完成后,我正在努力从 NavigationStack 中关闭 UIViewController。

登录屏幕是一个 UIViewController 用这行代码呈现

        let loginController = LoginController()
        self.present(loginController, animated: true, completion: nil)

然后我运行此代码以通过 firebase 登录用户。

        Auth.auth().signIn(withEmail: email, password: password) { (user, err) in
        if let err = err {
            print("Failed to sign in user with email", err )
        }

        //self.dismiss(animated: true, completion: nil)

        let userProfileVC = UserProfileController()
        let navController = UINavigationController(rootViewController: userProfileVC)

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

    }

如您所见,我尝试了 pushViewController 方法,还尝试了注释的 self.dismiss 方法?我所做的一切似乎都没有删除 loginController UIView 并将我带回 UINavigationController 主屏幕。谁能帮帮我,非常感谢。

标签: iosswiftuiviewcontroller

解决方案


您可能需要打印您的视图堆栈并将其展示给我们以便更好地理解,但请尝试以下方法之一:

navigationController?.popViewController(animated: true)

或者

navigationController?.popToRootViewController(animated: true)

第一种方法将处理最近推送的视图控制器,而第二种方法将删除除您寻找的导航控制器“主屏幕”之外的所有内容。


推荐阅读