简体   繁体   English

如何在 Swift 中以编程方式阻止 segue?

[英]How do I stop a segue from going through programmatically in Swift?

Here is the code:这是代码:

    @IBAction func loginTapped(_ sender: Any) {

    let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)

    Auth.auth().signIn(withEmail: email, password: password) { (result, error) in

        if error != nil {
            self.errorLabel.text = error!.localizedDescription
            self.errorLabel.alpha = 1
            print(error!.localizedDescription)
        }
        else {

            self.performSegue(withIdentifier: "loginSegue", sender: nil)

            print("User is signed in with Firebase.")
        }
    }
}

I have a segue, loginSegue , connected from the login button to the homeViewController .我有一个 segue, loginSegue ,从登录按钮连接到homeViewController Within in the if error statement I would like to stop the segue from going through because the user has not signed in. The goal here is not allow the user to go forward if they get an error.if error语句中,我想阻止 segue 进行,因为用户尚未登录。这里的目标是不允许用户在收到错误时转发 go。 Is there an "opposite" to the performSegue(withIdentifier: String, sender: Any?) ? performSegue(withIdentifier: String, sender: Any?)是否有“对立面”?

I think your button is connected to perform segue in storyboard.我认为您的按钮已连接以在 storyboard 中执行 segue。 So your button has two actions - one from storyboard to perform segue and second in your code.所以你的按钮有两个动作 - 一个来自 storyboard 执行 segue 和第二个在你的代码中。 Just remove the connection from storyboard and connect only UIViewControllers not with your button.只需从 storyboard 中删除连接并仅连接 UIViewControllers 而不是您的按钮。

First, there is no opposite of performSegue(withIdentifier: String, sender: Any?)?首先,performSegue(withIdentifier: String, sender: Any?)没有对立面?

But issue is not about it.但问题不在于它。 I think u you connected segue with login button and gave it a identify.我想你用登录按钮连接了 segue 并给了它一个标识。 If you give a button a segue directly, button always gonna show which page you connected with.如果你直接给一个按钮一个 segue,按钮总是会显示你连接到哪个页面。 Doing some operations in button action doesnt effect.在按钮动作中做一些操作没有效果。

You need to add this segue to FirstVc to SecondVc ( not to button) and then give identify this segue then control when login button tapped.您需要将此 segue 添加到 FirstVc 到 SecondVc (而不是按钮),然后识别此 segue 然后控制何时点击登录按钮。 If no error run segue with giving identify and show it.如果没有错误运行 segue 并给出标识并显示它。

You could override the shouldPerformSegue(withIdentifier:,sender:) method and return false if the login fails and you don't want to perform the segue.您可以覆盖shouldPerformSegue(withIdentifier:,sender:)方法并在登录失败并且您不想执行 segue 时返回 false。 Here's an example这是一个例子

override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool {
    if let ident = identifier {
        if ident == "YourIdentifier" {
            if loginSuccess != true {
                return false
            }
        }
    }
    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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