xcode 想让 UITextView 对返回键做出反应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7716104/
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-09-14 22:05:49  来源:igfitidea点击:

Want to make UITextView react to return key

objective-cxcode

提问by RaysonK

I'm trying to implement a program in Xcode that's somewhat like a command line. I basically have a UITextView that can take multiple lines of text. Right now, I have a button that will make the necessary changes after the user is done entering commands, but I want to be able to have a method that gets called after the user hits the return key in the UITextView, so basically it makes changes after each command. Is it possible to do this?

我正在尝试在 Xcode 中实现一个有点像命令行的程序。我基本上有一个可以接收多行文本的 UITextView。现在,我有一个按钮,可以在用户完成输入命令后进行必要的更改,但我希望能够在用户点击 UITextView 中的返回键后调用一个方法,因此基本上它会进行更改在每个命令之后。是否有可能做到这一点?

回答by Albert Renshaw

The BOOL method mentioned above is a wrong answer... for one the person is checking the text from the TextView the moment before it is updated so they are viewing the old text... Also the methods are out of date. This usage will work immediately once the return key is pressed (The current "answer" will not work until after the return key has been pressed and then ANOTHER key is pressed):

上面提到的 BOOL 方法是一个错误的答案......因为有人在更新之前检查 TextView 中的文本,所以他们正在查看旧文本......而且这些方法已经过时了。一旦按下返回键,此用法将立即生效(当前的“答案”在按下返回键然后按下另一个键后才会生效):

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"Return pressed");
    } else {
        NSLog(@"Other pressed");
    }
    return YES;
}

Don't forget to add the UITextViewDelegateto your .h file's protocols.

不要忘记将 加入UITextViewDelegate到您的 .h 文件的协议中。

@interface ViewController : UIViewController <UITextViewDelegate> {

and set yourTextView.delegate = self;in .m file!

yourTextView.delegate = self;在 .m 文件中设置!



/* 
note: This will also get called if a user copy-pastes just a line-break... 
 unlikely but possible. If you need to ignore pasted line-breaks for some 
 reason see here: http://stackoverflow.com/a/15933860/2057171 ... 
 Now for an unrelated-tip: If you want to accept pasted line breaks however 
 I suggest you add an "or" to the conditional statement and make it also 
 check if "text" isEqualToString @"\r" which is another form of line-break 
 not found on the iOS keyboard but it can, however, be found on a website 
 and copy-pasted into your textView.  If you want to accept pasted text 
 with a line-break at the end you will need to change the 
 "isEqualToString" code above to say "hasSuffix", this will check for 
 any string %@ with a "\n" at the end. (and again for "\r") but make 
 sure you don't call your "next" method until after `return YES;` 
 has been called and the text view has been updated, otherwise 
 you will get only the text that was there before the copy paste 
 since this is "shouldChangeTextInRange" method, not 
 "didChangeTextInRange", if you do this I suggest stripping the 
 "\n" or "\r" from the end of your final string after the copy-paste 
 was made and applied and the text was updated.
 */

回答by jbat100

If you set a delegate to the UITextView which implements

如果您将委托设置为实现的 UITextView

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

Then you can check if the last character is "\n", and get the text entered since the last command by doing

然后您可以检查最后一个字符是否为“\n”,并通过执行获取自上一个命令以来输入的文本

NSArray* components = [textView.text componentsSeparatedByString:@"\n"];
if ([components count] > 0) {
    NSString* commandText = [components lastObject];
    // and optionally clear the text view and hide the keyboard...
    textView.text = @"";
    [textView resignFirstResponder];
}

回答by James Webster

Note, I haven't tested this, just an idea:

请注意,我还没有测试过这个,只是一个想法:

- (void)textViewDidChange:(UITextView *)textView
{
    if ([[textView.text substringWithRange:NSMakeRange(textView.text.length - 1, 1)] isEqualToString:@"\n"])
    { 
       [textView resignFirstResponder];
       [self methodYouWantToCall];
    }
}

回答by PengOne

You can do this by setting up a delegate for the UITextView. See UITextViewDelegate Protocol Referencefor details on what can be done.

您可以通过为UITextView. 有关可以做什么的详细信息,请参阅UITextViewDelegate 协议参考