IOS textFieldDidBeginEditing

时间:2017-05-24 15:44:57

标签: ios swift uitextfield

我正在寻找一个我尝试使用的扩展程序的帮助,它是一个通过我的statusLabel.text发出错误的登录页面,例如,如果他们输入了错误的密码statusLabel .text会显示"电子邮件不正确请再试一次"

这是我的登录部分

NetworkInfo

我的扩展

@IBAction func login(_ sender: Any) {

    self.userName.resignFirstResponder()
    self.password.resignFirstResponder()
    self.repeatPassword.resignFirstResponder()
    self.passwordsStack.resignFirstResponder()


    self.progress.startAnimating()
    switch self.currentLoginType {

    case .login:

        let email = self.userName.text
        let password = self.password.text

        FirebaseManager.login(email: email!, password: password!, completion: { (status: Bool, error: Error?) in

            if status && error == nil {
                if FirebaseManager.isEmailVerified {

                    self.dismiss(animated: true, completion: nil)
                    print (error?.localizedDescription ?? "")

                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let LoginVC = storyboard.instantiateViewController(withIdentifier: "ReavealVC") as! SWRevealViewController
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.window?.rootViewController = LoginVC

                } else {

                    DispatchQueue.main.async {
                        self.statusLabel.text = "Your email is not verified. Please check your email"
                        self.progress.stopAnimating()
                        print ("have we stopped at email not verified")
                        self.clearLoginTextFields()
                    }
                }
            } else {

                DispatchQueue.main.async {
                    self.progress.stopAnimating()
                    self.statusLabel.text = FirebaseManager.StringFromError(error: error)
                    print("have we stopped at email after its not verified")
                    self.clearLoginTextFields()
                }

            }

        })


    case .signup:

        if self.password.text == self.repeatPassword.text{

            let email = self.userName.text
            let password = self.password.text

            FirebaseManager.LinkWithEmailAndPassword(email: email!, password: password!, completion: { (status: Bool, error: Error?) in

                if status && error == nil {

                    self.dismiss(animated: true, completion: nil)

                    self.progress.stopAnimating()
                    self.statusLabel.text = "Email verification sent, please check you inbox"
                    print(error?.localizedDescription ?? "")

                } else {

                    DispatchQueue.main.async {
                        self.progress.stopAnimating()
                        self.statusLabel.text = FirebaseManager.StringFromError(error: error)

                    }
                }
            })
            self.view.endEditing(true)
            clearLoginTextFields()

        }else{
            self.progress.stopAnimating()
            self.statusLabel.text = "Passwords do not match, please try again!"
        }

    case .forgotPassword:

        FirebaseManager.sendPasswordReset(email: self.userName.text!, completion: { (status:Bool) in

            DispatchQueue.main.async {
                self.progress.stopAnimating()
                self.statusLabel.text = status ? "Reset password sent" : "Error sending reset password"
            }
        })

        break
    }

}

我希望状态标签恢复为""一旦用户再次开始输入他们的凭证,但看起来我的textFieldDidBeginEditing没有被调用

1 个答案:

答案 0 :(得分:0)

您需要设置UITextField的委托。

userName.delegate = self
password.delegate = self

我假设两个文本字段都位于LoginViewController中。

除了这种方法,你可以没有代表的东西,并做:

userName.addTarget(self, action: #selector(textChanged(_:)), for: .editingChanged)

func textChanged(textField: UITextField) {

}

在设置“”之前,您可能需要检查是否显示消息:“电子邮件不正确,请再试一次”。