UITextView 在“完成”时放弃第一响应者;

发布于 2024-11-16 04:36:01 字数 318 浏览 4 评论 0原文

我已经在网上搜索了大约一个小时,但找不到任何代码来帮助我解决这个问题。

我有一个 UITextView,当用户按下键盘上的“完成”按钮时,我需要放弃第一响应者。

我见过这样的代码在互联网上流传:

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-11-23 04:36:02

您可以使用委托方法

- (void)textViewDidEndEditing:(UITextView *)textView { 
    [textView resignFirstResponder];
}

您必须将控制器设置为 TextView 的委托。
有关更多方法,您可以查看这里: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897

You can use the delegate method

- (void)textViewDidEndEditing:(UITextView *)textView { 
    [textView resignFirstResponder];
}

You have to set your controller as the delegate for the TextView.
For more methods you can have a look here: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897

今天小雨转甜 2024-11-23 04:36:01

实现 shouldChangeTextInRange: 委托方法。

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

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

Implement the shouldChangeTextInRange: delegate method.

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文