How do I detect the return key being pressed and r

2020-07-22 10:16发布

问题:

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