UITextView在'完成'上辞职第一响应者 [英] UITextView resign first responder on 'Done'

查看:71
本文介绍了UITextView在'完成'上辞职第一响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索大约一个小时,但找不到任何代码来帮助我。

I have been searching around the web for about an hour now, and cannot find any code to help me with this.

我有一个 UITextView 我需要在用户按下键盘上的完成按钮时辞职第一响应者。

I have a UITextView that I need to resign first responder of when the user presses the 'Done' button on their keyboard.

我看到代码浮动像这样在互联网上:

I have seen code floating around the internet like this:

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
 [textField resignFirstResponder];
 return NO;
}

但这不适用于 UITextView
简单地说,
如何判断用户何时按下键盘上的完成按钮?

But that will not work for a UITextView. Simply put, How can I tell when the user presses the done button on the keyboard?

推荐答案

实现 shouldChangeTextInRange:委托方法。

使用以下方法,解决方案仅适用于@\ n (新行字符)。

Use below approach and the solution work only with @"\n" (new line character).

//In you *.h file make sure you add
@interface v1ViewController : UIViewController <UITextViewDelegate>

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    myTextField.delegate = self;
}

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

            if ([text isEqualToString:@"\n"]) {

                [textView resignFirstResponder];
                // Return FALSE so that the final '\n' character doesn't get added
                return NO;
            }
            // For any other character return TRUE so that the text gets added to the view
            return YES;
    }

这篇关于UITextView在'完成'上辞职第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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