iPhone - UITextView 问题

发布于 2024-07-21 07:34:35 字数 71 浏览 3 评论 0 原文

这可能是一件容易做的事情,但我就是不明白 - 如何结束编辑文本视图? 如何让键盘消失? 或者我必须在它外面点击才能让它消失?

This is probably an easy thing to do, but I just can't figure it out - how do I end editing athe textview? how can I get the keyboard to disappear? or do I have to click outside it to make it go away?

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

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

发布评论

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

评论(4

满栀 2024-07-28 07:34:35

首先,(老实说)这样一个相当简单的问题让我想知道您是否尝试过阅读文档或在互联网上搜索。

搜索“Apple 文档 UITextView”将为您提供 此链接< /a> 到类文档。 同样,这里是 UITextViewDelegate。

搜索“UITextView simple example”会给你这个有用的示例

搜索“UITextView 关闭键盘”,第一个命中似乎是 准确回答您的问题。 (尽管他在返回键上关闭了键盘,但这可能不是您想要的。)(编辑 - 从您的第二条评论看来,这正是您想要的。)

PS 上面的人是正确的,如果有点简洁(可以理解) 。 您需要实现一个 UITextViewDelegate。 在该委托中,如果您想在返回键上隐藏键盘,请实现 shouldChangeTextInRange,查找 @"\n" 并在找到它时退出第一响应者。 或者,在用户界面中添加“完成编辑”按钮,并在用户按下该按钮时退出第一响应者。

First, a (to be honest) fairly simple question like this makes me wonder if you've tried reading the documentation, or searching on the internet.

Searching for "Apple documentation UITextView" gives you this link to the class documentation. Similarly, here is the documentation for the UITextViewDelegate.

Searching for "UITextView simple example" gives you this useful example.

Searching for "UITextView dismiss keyboard", the first hit seems to answer your question exactly. (Although he dismisses the keyboard on a return key, which may not be what you want.) (Edit - it seems from your second comment it's exactly what you want.)

P.S. The people above are correct, if a little terse (understandably). You need to implement a UITextViewDelegate. In that delegate, if you want to hide the keyboard on a return key, implement shouldChangeTextInRange, look for a @"\n" and resign first responder if you get it. Alternatively, add a "Done editing" button to your UI, and resign first responder if the user presses it.

物价感观 2024-07-28 07:34:35

非常简单:

[myTextField resignFirstResponder]; 

就可以了。

Very Easy:

[myTextField resignFirstResponder]; 

will do the trick.

半夏半凉 2024-07-28 07:34:35

结束编辑的一种方法是点击 textView 外部,这并不完全是微不足道的。 选择其他文本视图或文本字段或激活导航控件将触发...

- (void)textViewDidEndEditing:(UITextView *)textView

...您指定为 textView 委托的任何对象。 调用...来自行触发此操作

- (BOOL)endEditing:(BOOL)force

您可以通过在包含文本字段的视图上

。 假设我在 UITableViewCell 内有一个 UITextView (在 UITable 内)。 我想通过点击表格来结束编辑。 我可以这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTable)];
    [[self tableView] addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];
}

- (void)didTapTable
{
    [[self tableView] endEditing:YES];
}

现在每当我点击表格时,我都会结束编辑。 而且,正如其他人所说,在 textViewDidEndEditing 中,我应该确保调用 [textView resignFirstResponder];

One way to end editing, tapping outside the textView, is not entirely trivial. Selecting other text views or text fields or activating a navigation control will trigger...

- (void)textViewDidEndEditing:(UITextView *)textView

...on whatever object you've designated as the textView's delegate. You can trigger this yourself by calling...

- (BOOL)endEditing:(BOOL)force

...on the view that contains your text field.

Suppose I have a UITextView inside a UITableViewCell (inside a UITable). I want to enable editing to end by tapping the table. I could do this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTable)];
    [[self tableView] addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];
}

- (void)didTapTable
{
    [[self tableView] endEditing:YES];
}

Now whenever I tap my table, I end editing. And, as others have said, in textViewDidEndEditing I should be sure to call [textView resignFirstResponder];

意犹 2024-07-28 07:34:35
[yourTextField resignFirstResponder];

将使键盘消失并结束编辑。

[yourTextField resignFirstResponder];

will make the keyboard disappear and editing end.

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