【问题标题】:Is there a way to perform a conditional segue in a swift ViewController?有没有办法在 swift ViewController 中执行条件转场?
【发布时间】:2019-07-03 07:15:22
【问题描述】:

我正在尝试使用 Firebase 对用户登录进行身份验证。如果验证了用户的凭据,我将在 if-else 语句中使用 user 和 error 进入我的应用程序的主屏幕。但是,无论登录是否实际发生,performSegue(...) 都会执行。

我确定问题不在于 Firebase 中的验证/登录/注销问题或文本字段。

这是我的代码:

func handleSignIn(username:String, password:String) {
    Auth.auth().signIn(withEmail: username, password: password) { user, error in
        if error == nil && user != nil {
            //self.dismiss(animated: false, completion: nil)
            print("Logged in!")
            self.performSegue(withIdentifier: "toHome", sender: nil)
        } else {
            print("Error logging in: \(error!.localizedDescription)")
        }
    }
}

最好的,

代码爸爸

【问题讨论】:

  • 如果performSegue正在执行,那是否意味着你的代码每次都进入if块?这不是说明条件可能不对吗?

标签: swift firebase firebase-authentication segue


【解决方案1】:

我已经解决了这个问题。

上面的代码实际上是正确的和功能的。问题是在以下范围内调用了 handleSignIn 方法:

 @IBAction func didTapLogin(_ sender: Any) {...}

该按钮本身是带有标识符“toHome”的 segue 连接,因此无论 if/else 块评估为什么,应用程序都会进入主菜单。

一旦我删除并重新建立从 LoginViewController 到 HomeViewController 的 segue“toHome”(不是 HomeViewController 的登录按钮),这段代码就可以正常工作了。

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多