iphone - 无法摆脱 UITextView 中的键盘

标签 iphone keyboard uitextview hide

我有一张 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/

相关文章:

iphone - Cocos2d。六边形 map 示例

iphone - 编程内存警告 ios

c# - 为 Windows 7 桌面开发我自己的虚拟键盘

iphone - iOS-覆盖UITextView方法?

ios - 在 UITextView 中垂直居中 NSTextAttachment

ios - 如何增加短信iPhone应用程序的UITextView大小和UIToolBar高度?

iphone - Xcode错误语义错误值可能无法响应 'initWithFrame:image Name:'

iphone - 无法使用对 armv7s 的支持来更新我现有的应用程序(支持 armv6)?

linux - 在 Debian 中映射我的自定义键

ios - 如何更改 iOS 7 默认键盘上的某些击键?喜欢松弛