我有一张 table ,在其中一个单元格中保留了一个 UITextView。当用户点击它时,键盘就会出现。我尝试了各种方法来摆脱它。我的第一个方法是检测 UITextView 的表格单元格上的点击,但由于 TextView 占用了大部分内容,因此它不合适。然后我尝试向工具栏添加一个按钮,每当用户按下它时,键盘就会随着 resignFirstResponder 消失,但它不起作用。看来只有当我与 UITextView resignFirstResponder 处于同一 View 时才有效。那么如何从不同的角度摆脱键盘呢?
谢谢。
最佳答案
下面的方法使用 Return 键来关闭 UITextView 键盘。
在你的 Controller 中,将 UITextView 的 Delegate 设置为 self 像这样:
myTextView.delegate=self;
然后添加这个方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
如果您不使用 Return 键作为真正的 Return 键(例如添加新行),这应该有效。
关于iphone - 无法摆脱 UITextView 中的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1456599/