简体   繁体   中英

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

}

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

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.

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