如何在UITextField返回键上添加操作? [英] how to add an action on UITextField return key?

查看:90
本文介绍了如何在UITextField返回键上添加操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

确保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
}

这篇关于如何在UITextField返回键上添加操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆