且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在UITextField返回键上添加操作?

更新时间:2022-06-23 06:04:07

确保self订阅 UITextFieldDelegate 并初始化inputText:

Ensure "self" subscribes to UITextFieldDelegate and initialise inputText with:

self.inputText.delegate = self;

将以下方法添加到self:

Add the following method to "self":

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.inputText) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

或者在Swift中:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == inputText {
        textField.resignFirstResponder()
        return false
    }
    return true
}