Segue没有被执行

时间:2018-03-06 10:55:13

标签: ios swift

在我的第一个ViewController中,我正在检查用户是否已使用Firebase登录,如果他/她是,则重定向到主交换机,如果没有,则重定向到登录视图。但是,欢迎视图只会保持在视图中,并且不会发生重定向。

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

}

代码已更改

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

}

2 个答案:

答案 0 :(得分:2)

您的代码不会重定向到任何视图,因为您已在viewWillAppear中编写代码。在视图出现之前无法执行Segue 所以你必须在viewDidAppear中执行segue。 如果其他一切都正确(segue名称和连接),那么它肯定会起作用。

试试这个 -

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

答案 1 :(得分:0)

确保故事板中有正确连接的segue,并且segue的名称已正确设置为" goToLoginVC"。

另外,请提供点击与btnLoginWasPressed操作相关联的按钮的结果,以验证segue在该情况下是否正常工作,而不是viewDidLoad功能。