How do I detect the return key being pressed and respond to it using the UIKeyInput protocol?

[亡魂溺海] 提交于 2019-12-06 07:52:32

问题


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.


回答1:


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];
}

Hope it helps



来源:https://stackoverflow.com/questions/9592593/how-do-i-detect-the-return-key-being-pressed-and-respond-to-it-using-the-uikeyin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!