简体   繁体   中英

Swift - pushing ViewController not working after presenting ViewController again

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 :)

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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM