gpt4 book ai didi

iOS 文本字段DidBeginEditing

转载 作者:行者123 更新时间:2023-11-30 12:26:20 25 4
gpt4 key购买 nike

我正在寻找有关我尝试使用的扩展的一些帮助,它是一个登录页面,通过我的 statusLabel.text 发出错误,例如,如果他们输入不正确的密码,statusLabel.text 将显示“电子邮件地址不正确,请重试”

这是我的登录部分

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

}

还有我的扩展

extension LoginViewController: UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

textField.resignFirstResponder()

return true
}

func textFieldDidEndEditing(_ textField: UITextField) {

textField.resignFirstResponder()

}

func textFieldDidBeginEditing(_ textField: UITextField) {

statusLabel.text = ""
}
}

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

最佳答案

您需要设置 UITextFields 的委托(delegate)。

userName.delegate = self
password.delegate = self

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

除了这种方法之外,您可以不使用委托(delegate)的东西并执行以下操作:

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

func textChanged(textField: UITextField) {

}

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

关于iOS 文本字段DidBeginEditing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44163044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com