jgravois
jgravois

Reputation: 2579

Segue is not performed

In my first ViewController, I am checking to see if the user is already logged in with Firebase and if he/she is, redirect to the main switchboard and if not, redirect to the login view. However, the Welcome View just stays in view and no redirect happens.

import UIKit
import Firebase

class WelcomeVC: UIViewController {

    @IBOutlet weak var btnLogin: RoundedShadowButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        if(Auth.auth().currentUser?.uid != nil) {
            //user is logged in
            performSegue(withIdentifier: "goToSwitchboardVC", sender: self)
        }
        else {
            //user in not logged in
            performSegue(withIdentifier: "goToLoginVC", sender: self)
        }
    }

    @IBAction func btnLoginWasPressed(_ sender: Any) {
        performSegue(withIdentifier: "goToLoginVC", sender: self)
    }

}

CODE CHANGED

import UIKit
import Firebase

class WelcomeVC: UIViewController {

    @IBOutlet weak var btnLogin: RoundedShadowButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        print("AUTH USER")
        print(Auth.auth().currentUser)
        print(Auth.auth().currentUser?.uid)

        if(Auth.auth().currentUser?.uid != nil) {
            //user is logged in
            performSegue(withIdentifier: "goToSwitchboardVC", sender: self)
        }
        else {
            //user in not logged in
            performSegue(withIdentifier: "goToLoginVC", sender: self)
        }
    }

    @IBAction func btnLoginWasPressed(_ sender: Any) {
        performSegue(withIdentifier: "goToLoginVC", sender: self)
    }

}

Upvotes: 0

Views: 162

Answers (2)

Preeti Rani
Preeti Rani

Reputation: 695

Your code will not redirect to any view because you have written it in viewWillAppear. Segue cannot be performed before view appeared So you must perform segue in viewDidAppear. If everything else is correct (segue name and connections) then definitely it will work.

Try this -

override func viewDidAppear(_ animated: Bool) {
    print("AUTH USER")
    print(Auth.auth().currentUser)
    print(Auth.auth().currentUser?.uid)

    if(Auth.auth().currentUser?.uid != nil) {
        //user is logged in
        performSegue(withIdentifier: "goToSwitchboardVC", sender: self)
    }
    else {
        //user in not logged in
        performSegue(withIdentifier: "goToLoginVC", sender: self)
    }
}

Upvotes: 3

davidethell
davidethell

Reputation: 12038

Be sure you have a properly connected segue in your Storyboard and that the name of the segue is correctly set to "goToLoginVC".

Also please provide the results of tapping the button that is connected to the btnLoginWasPressed action to verify that the segue is working in that circumstance and just not in your viewDidLoad function.

Upvotes: 0

Related Questions