ios 如何让 UITextView“完成”按钮退出FirstResponder?

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

How to make UITextView "Done" button resignFirstResponder?

iphoneinterface-builderiosuitextviewiboutlet

提问by Jason

I am trying to make my editable UITextViewresign the keyboard (resignFirstResponder) when the user taps "Done." Using a UITextField, I have been able to do this with the following code:

当用户点击“完成”时,我试图让我的可编辑UITextView退出键盘 ( resignFirstResponder)。使用 a UITextField,我已经能够使用以下代码来做到这一点:

- (IBAction)doneEditing:(id)sender {
    [sender resignFirstResponder];
}

... and then to attach it to the relevant UITextFieldin Interface Builder to the action "Did End on Exit."

...然后将其附加到UITextFieldInterface Builder 中的相关操作“Did End on Exit”。

However, with a UITextView, I can't seem to access the "Did End on Exit" action. Any suggestions on how to make this happen?

但是,使用UITextView,我似乎无法访问“退出时确实结束”操作。关于如何做到这一点的任何建议?

采纳答案by Jordan

new Answer

新答案

On your View, you'd have a UIBarButton ("Done") that is connected to the IBAction below:

在您的视图上,您​​将有一个连接到下面 IBAction 的 UIBarButton(“完成”):

- (IBAction)doneEditing:(id)sender {
    [textView resignFirstResponder];
}

Where textView is your textView outlet defined in your .h file and connected in Storyboard or .xib file. Like this:

其中 textView 是在 .h 文件中定义并在 Storyboard 或 .xib 文件中连接的 textView 插座。像这样:

@property (retain, nonatomic) IBOutlet UITextView *textView;

old Answer

旧答案

Check the following:

检查以下内容:

  1. Is UITextViewDelegate specified in .h
  2. Implement delegate method for uitextview: textViewShouldEndEditing, return YES
  3. make sure your .m (controller) is the delegate for uitextview in IB
  4. resignFirstResponder should now work.
  1. .h 中是否指定了 UITextViewDelegate
  2. 为 uitextview 实现委托方法:textViewShouldEndEditing,返回 YES
  3. 确保您的 .m(控制器)是 IB 中 uitextview 的委托
  4. resignFirstResponder 现在应该可以工作了。

回答by Rich Lowenberg

The accepted answer didn't work for me. Instead, the following delegate method should be invoked like this:

接受的答案对我不起作用。相反,应该像这样调用以下委托方法:

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

Paste that into the class that you assign to be the UITextView delegate and it'll work.

将其粘贴到您指定为 UITextView 委托的类中,它就会起作用。

回答by Axel Guilmin

You can implement UITextViewDelegateand wait for "\n", in Swift 4 :

您可以UITextViewDelegate在 Swift 4 中实现并等待 "\n" :

    myTextView.delegate = self

// ...

extension MyViewController : UITextViewDelegate {

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            return false
        }

        return true
    }
}

回答by Damien Praca

To have the done button dismiss the keyboard and resignFirstResponderyou have to implement a delegate method.

要让完成按钮关闭键盘,resignFirstResponder您必须实现一个委托方法。

  1. In your .h implement the UITextFieldDelegate

    @interface YourViewController : UIViewController <UITextFieldDelegate>
    
  2. Then implement the textFieldShouldReturn in your .m

    -(BOOL) textFieldShouldReturn:(UITextField *)textField
    {
    [textField resignFirstResponder];
    return YES;
    }
    
  3. Don't forget to link the delegate of UITextField to the file's Owner (very important)

  1. 在你的 .h 中实现 UITextFieldDelegate

    @interface YourViewController : UIViewController <UITextFieldDelegate>
    
  2. 然后在你的 .m 中实现 textFieldShouldReturn

    -(BOOL) textFieldShouldReturn:(UITextField *)textField
    {
    [textField resignFirstResponder];
    return YES;
    }
    
  3. 不要忘记将 UITextField 的委托链接到文件的所有者(非常重要)