Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-Firebase身份验证登录功能在演示segue内,如何使用shouldPerformSegue阻止segue执行?_Swift_Storyboard_Closures_Segue_Grand Central Dispatch - Fatal编程技术网

Swift-Firebase身份验证登录功能在演示segue内,如何使用shouldPerformSegue阻止segue执行?

Swift-Firebase身份验证登录功能在演示segue内,如何使用shouldPerformSegue阻止segue执行?,swift,storyboard,closures,segue,grand-central-dispatch,Swift,Storyboard,Closures,Segue,Grand Central Dispatch,我想使用故事板segue(modally呈现)呈现新的视图控制器,但shouldPerform函数中的验证是Firebase授权函数signIn,但由于尾部闭包直接返回编译器,因此在signIn函数对标志进行任何更改之前,它会返回false var shouldPerformSegue = false override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

我想使用故事板segue(modally呈现)呈现新的视图控制器,但shouldPerform函数中的验证是Firebase授权函数signIn,但由于尾部闭包直接返回编译器,因此在signIn函数对标志进行任何更改之前,它会返回false

var shouldPerformSegue = false
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    guard let email = loginemail.text, let pass = loginpassword.text, email != "", pass != "" else {
        return false
    }
    Auth.auth().signIn(withEmail: email, password: pass) { (result, error) in
        if let error = error{
            TWMessageBarManager().showMessage(withTitle: "invalid email or pass", description: error.localizedDescription, type: .error)
            self.shouldPerformSegue = false

        } else {
        self.shouldPerformSegue = true
        }
    }
    return shouldPerformSegue
}

我的目标是使用故事板序列(特别是非自定义序列)来解决这个问题,可能吗?

尝试使用完成处理程序

func didUserLoggedInSuccessfully(success:@escaping (NSDictionary) -> Void)
    {
        Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
            if error == nil {
                success(true)
            }
            else{
                //Tells the user that there is an error and then gets firebase to tell them the error
                success(false)
            }
        }
    }
Usgae

self.didUserLoggedInSuccessfully { (success) in
            if success {
                /// Perform Segue
            }
            else{
                /// Do not Perform
            }
        }

这里有更准确的答案。这里我将
@escaping
类型从
NSDictionary
更改为
(Bool,Error?
),因为我们需要返回带有结果的完成时钟

func didUserLoggedInSuccessfully(success:@escaping (Bool, Error?) -> Void)
{
    Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
        if error == nil {
            success(true,nil)
        }
        else{
            success(false,error)
        }
    }
}


self.didUserLoggedInSuccessfully { (result,error) in
    if result {
        /// Perform Segue
    }
    else{
        /// Do not Perform
        TWMessageBarManager().showMessage(withTitle: "invalid email or pass", description: error.localizedDescription, type: .error)
    }
}

我是否应该调用shouldPerformWithIdentifier:sender:中的函数?是的,您可以,如果处理程序返回Success==True,您需要执行segue,只需在我编写的地方编写performsgue///perform segue谢谢您的帮助,但我想我仍在试图弄清楚segue的生命周期是什么。我在if success{self.performsgue(带标识符:String,sender:Any?)中编写了override func shouldPerformSegue(带标识符:String,sender:Any?)->Bool{guard let email=loginemail.text,let pass=loginpassword.text,email!=“”,pass!=“”否则{return false}didUserLoggedInSuccessfully{(成功){self.performsgue)(带标识符:“loginSegue”,sender:UIViewController())}}返回false}它可以工作。但是我有很多冗余,我想如果我调用performsguewithidentifier:sender:function,它似乎不会调用shouldPerformSeguewithIdentifier。我说的对吗?