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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11553396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:18:47  来源:igfitidea点击:

how to add an action on UITextField return key?

iosobjective-ciphoneswiftuitextfield

提问by razibdeb

I have a button and text textfield in my view. when i click on the textfield a keyboard appears and i can write on the textfield and i also able to dismiss the keyboard by clicking on the button by adding:

我的视图中有一个按钮和文本文本字段。当我单击文本字段时,会出现一个键盘,我可以在文本字段上书写,我还可以通过添加以下按钮来关闭键盘:

[self.inputText resignFirstResponder];

Now I want to enable return key of keyboard. when i will press on the keyboard keyboard will disappear and something will happen. How can I do this?

现在我想启用键盘的返回键。当我按下键盘时键盘会消失并且会发生一些事情。我怎样才能做到这一点?

回答by Ander

Ensure "self" subscribes to UITextFieldDelegateand initialise inputText with:

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

self.inputText.delegate = self;

Add the following method to "self":

将以下方法添加到“self”:

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

Or in Swift:

或者在 Swift 中:

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

回答by Fangming

With extension style in swift 3.0

使用 swift 3.0 中的扩展样式

First, set up delegate for your text field.

首先,为您的文本字段设置委托。

override func viewDidLoad() {
    super.viewDidLoad()
    self.inputText.delegate = self
}

Then conform to UITextFieldDelegatein your view controller's extension

然后UITextFieldDelegate在您的视图控制器的扩展中符合

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

回答by Malfunction

While the other answers work correctly, I prefer doing the following:

虽然其他答案正常工作,但我更喜欢执行以下操作:

In viewDidLoad(), add

在 viewDidLoad() 中,添加

self.textField.addTarget(self, action: #selector(onReturn), for: UIControl.Event.editingDidEndOnExit)

self.textField.addTarget(self, action: #selector(onReturn), for: UIControl.Event.editingDidEndOnExit)

and define the function

并定义函数

@IBAction func onReturn() {
    self.textField.resignFirstResponder()
    // do whatever you want...
}

回答by Adobels

Use Target-Action UIKit mechanism for "primaryActionTriggered" UIEvent sent from UITextField when a keyboard done button is tapped.

当点击键盘完成按钮时,将 Target-Action UIKit 机制用于从 UITextField 发送的“primaryActionTriggered”UIEvent。

textField.addTarget(self, action: Selector("actionMethodName"), for: .primaryActionTriggered)