如何 link textFiledshouldReturn func to Action Button

How to link textFiledshouldReturn func to Action Button

如何使 TextField 键盘中的 return 键与登录按钮的功能相同

请帮助我,因为我无法删除 textFiledShouldReturn 函数中的操作按钮

检查下面我的代码:

class Sign_in: UIViewController, UITextFieldDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    self.Password.delegate = self
    self.Password.returnKeyType = .go
  }


// To make the return Key in Keyboard do dismiss when pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    return  true
}

//授权码

@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password : UITextField!

@IBAction func signIN (_ sender : UIButton) {

    Auth.auth().signIn(withEmail: self.Email.text!, password: self.Password.text!) { (status, error) in
        if error == nil {
            print ("Sign in succeeded")
        } else {
            print ("username or password mismatched: " + error.debugDescription)
        }
    }
}

我知道我可以将整个 Sign in Auth 代码复制到整个 textFiledShouldReturn func,但这太糟糕了...那么有没有更好的方法来做到这一点?

您可以为您的身份验证登录创建一个方法,并从您的 textFieldShouldReturn 和按钮操作调用该方法,如下所示:

class Sign_in: UIViewController {
    @IBAction func signIN (_ sender : UIButton) {
        authLogin()
    }

    func authLogin() {
         Auth.auth().signIn(withEmail: self.Email.text!, password: self.Password.text!) { (status, error) in
            if error == nil {
                print ("Sign in succeeded")
            } else {
                print ("username or password mismatched: " + error.debugDescription)
            }
        }
    }
}

extension Sign_in : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        authLogin()
        return  true
    }
}