无法摆脱 UITextView 中的键盘

发布于 2024-08-05 04:27:07 字数 264 浏览 2 评论 0原文

我有一张桌子,在其中一个单元格中保留了 UITextView。当用户点击它时,键盘就会出现。我尝试了各种方法来摆脱它。我的第一个方法是检测 UITextView 的表格单元格上的点击,但由于文本视图占用了大部分内容,因此它不合适。然后我尝试向工具栏添加一个按钮,每当用户按下它时,键盘就会随着 resignFirstResponder 消失,但它不起作用。似乎只有当我与 UITextView resignFirstResponder 处于同一视图时才有效。那么如何从不同的角度摆脱键盘呢?

谢谢。

I have a table and in one of the cells I keep a UITextView. When the user taps it the keyboard appears. I was trying various ways to get rid of it. My first approach was to detect a tap on the table cell of the UITextView, but since the text view takes most of it, it's not suitable. Then I tried to add a button to the toolbar and whenever the user presses it, the keybord disappears with resignFirstResponder, but it won't work. It seems that only when I'm in the same view as the UITextView resignFirstResponder works. So how can I get rid of the keyboard from a different view?

Thanks.

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

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

发布评论

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

评论(5

一直在等你来 2024-08-12 04:27:08

在不知道控件的大小和意图的情况下,很难提出可靠的建议。但是,如果您正在寻找只读文本视图,为什么不考虑 UILabel?如果它将包含大量只读文本并且您需要丰富的格式,请考虑使用具有您的格式的 UIWebView。

当然,这个答案可能完全不合适。您能否澄清您的意图,也许可以显示正在进行的应用程序的屏幕截图(当然,尚未开发)?

Without knowing the size of the controls, nor the intent, it's a bit hard to make a solid recommendation. However, if you're looking for a read-only text view, why not consider a UILabel? If it's going to contain a great deal of read-only text and you need rich formatting, consider a UIWebView with your formatting.

Of course, this answer could be completely inappropriate. Can you clarify your intentions, maybe show a screen shot of the app-in-progress (untapped, of course)?

失而复得 2024-08-12 04:27:08

尝试在界面生成器的文本视图属性窗口中取消选中“可编辑”选项。

如果用户单击您的 TextView,这将阻止键盘显示

try unchecking the 'Editable' option in the Text View Attributes window in Interface Builder.

this stops the keyboard from showing up if the user clicks on your TextView

以为你会在 2024-08-12 04:27:08

我曾经遇到过此类问题,以下方法

- (BOOL)textFieldShouldReturn: tf_some_text_field {

[tf_some_text_field resignFirstResponder];
[tf_another_text_field_in_the_same_view resignFirstResponder];

return YES;
}

希望对您有所帮助...

i've use for that kind of problem, the folowing method

- (BOOL)textFieldShouldReturn: tf_some_text_field {

[tf_some_text_field resignFirstResponder];
[tf_another_text_field_in_the_same_view resignFirstResponder];

return YES;
}

hope it helps...

寄居人 2024-08-12 04:27:07

下面的方法使用 Return 键来关闭 UITextView 键盘。

在您的控制器中,将 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 键(例如添加新行),这应该可以工作。

The method below uses the Return Key to dismiss the UITextView Keyboard.

In your Controller, set the UITextView's Delegate to self
like this:

myTextView.delegate=self;

Then add this method:

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

This should work if you're not using the Return key as a true Return key (e.g. adding new line).

蘸点软妹酱 2024-08-12 04:27:07

你是什​​么意思“这不会起作用”。如果 txtView 是您的 UITe4xtView,您的意思是将该按钮与具有 [txtView resignFirstResponder] 的操作关联起来不起作用吗?我以前使用过这段代码并且它对我有用

What do you mean "it won't work". If txtView is your UITe4xtView, do you mean that associating the button with an action that has [txtView resignFirstResponder] wont work? I've used this code before and it works for me

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