如何在 Enter 按键时关闭键盘
我想在按 RETURN 键时关闭键盘。
我尝试过将按钮放在视图的背面。
但是我怎样才能通过按 RETURN 键来做到这一点呢?
I want to dismiss my keyboard as I press RETURN key.
I have tried by putting button in view's back side.
But how can I do this by pressing RETURN key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要忘记添加委托
UITextFieldDelegate
Don't forget to add the delegate
UITextFieldDelegate
我假设您正在谈论
UITextField
而不是UITextView
因为您的问题不太清楚?如果是这样,请确保您的类在接口文件中标记为UITextFieldDelegate
,然后您应该实现如下两个委托方法,
但是如果您使用的是
UITextView
那么事情就有点复杂了。UITextViewDelegate
协议缺少与textFieldShouldReturn:
方法等效的方法,大概是因为我们不应该期望 Return 键是用户希望停止编辑文本的信号。多行文本输入对话框(毕竟,用户可能希望通过按 Return 来插入换行符)。但是,有多种方法可以解决
UITextView
无法使用键盘辞去第一响应者身份的问题。通常的方法是当UITextView
显示弹出键盘时在导航栏中放置一个 Done 按钮。点击后,此按钮会要求文本视图辞去第一响应者的角色,然后关闭键盘。但是,根据您规划界面的方式,您可能希望当用户点击
UITextView
本身之外时,UITextView
退出。为此,您需要对UIView
进行子类化以接受触摸,然后指示文本视图在用户点击视图本身外部时退出。创建一个新类,
然后在实现中实现
touchesEnded:withEvent:
方法并要求UITextView
辞去第一响应者身份。添加类后,您需要保存所有更改,然后进入 Interface Builder 并单击您的视图。在实用工具面板中打开身份检查器,并将 nib 文件中的视图类型更改为
CustomView
而不是默认的UIView
类。然后在 Connections Inspector 中,将textView
出口拖动到UITextView
。执行此操作后,一旦您重建应用程序,活动 UI 元素外部的触摸现在将关闭键盘。但请注意,如果您要子类化的 UIView 位于其他 UI 元素“后面”,则这些元素将在触摸到达 UIView 层之前拦截它们。因此,虽然这个解决方案很优雅,但它只能在某些情况下使用。在许多情况下,您必须诉诸暴力方法,在导航栏中添加完成按钮来关闭键盘。I'm presuming you're talking about a
UITextField
rather than aUITextView
as your question isn't that clear? If so then ensure your class is marked as aUITextFieldDelegate
in the interface file,and then you should implement the two delegate methods as below,
However if you're using a
UITextView
then things are a bit more complicated. TheUITextViewDelegate
protocol lacks the equivalent to thetextFieldShouldReturn:
method, presumably since we shouldn’t expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return).However, there are several ways around the inability of the
UITextView
to resign as first responder using the keyboard. The usual method is to place a Done button in the navigation bar when theUITextView
presents the pop-up keyboard. When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard.However, depending on how you’ve planned out your interface, you might want the
UITextView
to resign when the user taps outside theUITextView
itself. To do this, you’d subclassUIView
to accept touches, and then instruct the text view to resign when the user taps outside the view itself.Create a new class,
Then, in the implementation, implement the
touchesEnded:withEvent:
method and ask theUITextView
to resign as first responder.Once you’ve added the class, you need to save all your changes, then go into Interface Builder and click on your view. Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your
CustomView
rather than the defaultUIView
class. Then in the Connections Inspector, drag thetextView
outlet to theUITextView
. After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. Note however that if theUIView
you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. So while this solution is elegant, it can be used in only some situations. In many cases, you’ll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard.我希望你已经完成了
UIViewController
和yourTextField.delegate=self
;然后在委托方法中
I hope you have done
UIViewController <UITextFieldDelegate>
andyourTextField.delegate=self
;and then in the delegate method
确保您的视图控制器类是
UITextField
的委托,然后使用该类中的委托方法:Make sure your view controller class is a delegate for your
UITextField
and then use the delegate method in that class: