Swift - 点击键盘“完成”时点击一个按钮

时间:2016-11-11 20:46:38

标签: ios swift uibutton uitextfielddelegate

我遇到这种情况:

screenshot of my app

我希望当用户从键盘上点击“完成”时,它会自动点击“登录/注册”按钮。

我读过:

我的班级应该遵循协议UITextFieldDelegate

设置密码字段passwordTextField.delegate = self

的代理人

然后做这样的事情:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // releasing the focus on the textField and hiding the keyboard
        passwordTextField.resignFirstResponder()
        self.tapOnSignIn()
        return true
    }

然后我创建了tapOnSignIn函数

func tapOnSignIn() {
        self.signinupButton.sendActions(for: .touchDown)
    }

但是当我点击键盘上的“完成”按钮时,没有任何反应。

1 个答案:

答案 0 :(得分:0)

找到答案:

我需要为我的按钮调用操作并将self作为参数传递:

self.signinupTapped(self)

现在我的职能是:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // releasing the focus on the textField and hiding the keyboard
    passwordTextField.resignFirstResponder()
    self.signinupTapped(self)
    return true
}