0

The question sounds a bit confusing but I don't know how to describe it better.. Let me explain:

The first ViewController that gets presented is FirstLaunchVC, where he user types in his email and if he is registered he get's to LoginVC and from there he get's to MainVC. Everything is working fine.

In MainVC the user can sign out and get's back to FirstLaunchVC. However, after doing the weiterButton which should bring the user to LoginVC is not doing anything.

FirstLaunchVC:

@objc func weiterButtonTapped() {

    email = emailTextfield.text!.trimmingCharacters(in: .whitespacesAndNewlines)

    //prüfen, ob Email schon registriert ist
    Auth.auth().fetchSignInMethods(forEmail: email) { (methods, error) in

        //Email ist noch nicht registriert -> sign up
        if methods == nil {

            let SignUpView = self.storyboard?.instantiateViewController(withIdentifier: "SignUpVC") as! SignUpViewController

            SignUpView.email = self.email

            self.navigationController?.pushViewController(SignUpView, animated: false)

        }
        //Email ist registriert -> login
        else {
            print("hi")

            self.view.endEditing(true)

            let LoginView = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController

            LoginView.email = self.email

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

Main Problem:

The print(hi) is printing but pushViewController is not working after signing out.

LoginVC:

func transitionToHome () {

    let homeVC =
    storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.homeViewController) as? MainViewController
    let navigationController = UINavigationController(rootViewController: homeVC!)

    view.window?.rootViewController = navigationController
    view.window?.makeKeyAndVisible()
}

MainVC:

This is where the user can sign out.

@objc func signoutButtonTapped() {
    UserDefaults.standard.setIsLoggedIn(value: false)
    UserDefaults.standard.synchronize()

    let firstLaunchVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstLaunchVC")

    self.navigationController?.present(firstLaunchVC, animated: true)
}

I tried to explain the problem the best I can. If anything is still unclear just let me know. I am happy for any help :)

Chris
  • 1,828
  • 6
  • 40
  • 108

1 Answers1

-1

if after sign out weiterButtonTapped() called,

you should put your pushController line in dispatch that's because controller be fully deallocated:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
        self.navigationController?.pushViewController(LoginView, animated: true)
    }
Reza Khonsari
  • 479
  • 3
  • 13
  • this code is running in the main thread is no necessary to check for it, and also why the delay of 3 second when it could be done ASAP. when the button is touched. – Yoel Jimenez del valle Jan 24 '20 at 23:07
  • @kjoe navigationcontroller can present controller, it's not 3 second it's 0.3 second – Reza Khonsari Jan 24 '20 at 23:10
  • I don’t see a present method in UINavigationControlleronly a show a push remember in uistotyboard a navigation controller can not modalu present a view controller and using 0.3 is not a real difference better use DispatchQueue.main.async{} – Yoel Jimenez del valle Jan 25 '20 at 00:19