如何检测按下的返回键并使用UIKeyInput协议对其进行响应? [英] How do I detect the return key being pressed and respond to it using the UIKeyInput protocol?

查看:127
本文介绍了如何检测按下的返回键并使用UIKeyInput协议对其进行响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,显示我希望用户能够编辑的列表。为了节省空间,并使我的视图更容易在眼睛上,我创建了一个符合UIKeyInput协议的自定义工具栏,这样我就可以拉出键盘而不必使用任何文本字段。到现在为止还挺好。我有一个可变字符串来处理来自键盘的输入:

I have a table view that displays a list that I want the user to be able to edit. In order to save space, and to make my view easier on the eyes, I have created a custom toolbar that conforms to the UIKeyInput protocol so that I can pull up a keyboard without having to use any text fields. So far so good. I have a mutable string that is handling the input from the keyboard:

- (void)insertText:(NSString *)text {
    if (!itemForList) {
        itemForList = [NSMutableString string];
    }    
    [itemForList appendString:text];

}

我无法弄清楚怎么办当用户按下返回时检测到。这很重要,因为我需要能够获取用户输入的字符串并将其添加到表视图显示的可变数组中,然后重置字符串以处理新输入。我非常感谢这个领域的任何帮助。谢谢大家。

The thing that I can't figure out how to do is detect when the user presses return. This is important because I need to be able to take the string that the user typed in and add it to the mutable array that the table view is displaying from, and then reset the string to handle new input. I would greatly appreciate any help in this field. Thanks guys.

推荐答案

尝试使用转义字符?示例:

Did try using escape characters? Example:

- (void)insertText:(NSString *)text {
  if ([text isEqualToString:@"\n"]) {
    //do whatever you want to do when user taps the return key
  }
  if (!itemForList) {
    itemForList = [NSMutableString string];
  }
  [itemForList appendString:text];
}

希望有所帮助

这篇关于如何检测按下的返回键并使用UIKeyInput协议对其进行响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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