iphone - UITextView 在 'Done' 上退出第一响应者

标签 iphone objective-c interface-builder uitextview uikeyboard

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

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

我在网上看到过这样的代码:

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

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

最佳答案

实现 shouldChangeTextInRange: 委托(delegate)方法。

使用以下方法,解决方案仅适用于@"\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;
    }

关于iphone - UITextView 在 'Done' 上退出第一响应者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411858/

相关文章:

iphone - 界面生成器警告

ios - 如何在 iOS 中以 pageadmin 身份在 Facebook 页面上发布状态提要?

iphone - 如果您正在为越狱的 iPhone 进行编译,您应该针对哪个内核?特别是 iPhone 4s

iphone - 检测滑动 Action 以转到 ipad Web 应用程序上的另一个网页

iphone - 如何在 objective-c 中使用属性的实际示例?

ios - 用 XIB 替换 Storyboard

ios - 子类化后的AVPlayerViewController横向定位

ios - Dealloc 释放后不会立即调用

ios - 我如何打印出存储在数组中的对象的内容,而不是像这样的内容 <(Object Name) : 0x10010ff10>?

ios - 在界面生成器中使用现有的自定义 UIView