Can't dismiss UIViewController after Successful Login

Viewed 94

I'm struggling to dismiss a UIViewController from the NavigationStack after login is completed.

The login screen is a UIViewController is presented with this line of code

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

And then I run this code to log the user in via 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)

    }

As you can see I have tried the pushViewController method, and have also tried the commented self.dismiss method? Nothing I do seems to remove the loginController UIView and take me back to the UINavigationController home screen. Can anyone help me out, thanks a lot.

3 Answers

you can do as this

self.dismiss(animated: true, completion: {
let userProfileVC = UserProfileController()
        let navController = UINavigationController(rootViewController: userProfileVC)

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

You might have to print your viewstack and show it to us in order to understand better but try one of these methods:

navigationController?.popViewController(animated: true)

or

navigationController?.popToRootViewController(animated: true)

The first method will dispose of the most recently pushed view controller, while the second will remove all but the navigation controllers "home screen" which you seek.

You can not push to particular controller while any controller is present on current controller. so you can use protocol delegate on controller which is present current controller. when delegate method called then you can push to your controller.

Related