我已经在网上搜索了大约一个小时,但找不到任何代码来帮助我解决这个问题。
我有一个 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/