如何以编程方式关闭键盘? [英] How can I dismiss the keyboard programmatically?

查看:169
本文介绍了如何以编程方式关闭键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来确定当前在视图中选择的 UITextField 。这是否可能没有传递引用或标记?

I need a way of determining the UITextField that is currently selected in a view. Is this possible without passing a reference or tag?

更具体地说,我需要能够知道选择了哪个 UITextField ,以便我可以隐藏键盘。我需要这个的原因是因为我想创建一个 UIToolbar 添加到所有 UITextField '作为输入附件。在这个 UIToolbar 我将添加一个完成按钮,当按下这应该隐藏当前选择的键盘UITextField

To be more specific I need to be able to tell which UITextField is selected so that I can hide the keyboard. The reason I need this is because I want to create a UIToolbar to add to all the UITextField's as an input accessory. On this UIToolbar I will add a 'Done' button, when pressed this should hide the keyboard for the currently selected UITextField.

推荐答案

有一个委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField

Apple文档: / p>

Apple Docs:


此方法通知委托人,指定的文本字段只是
成为第一个响应者。您可以使用此方法更新
委托的状态信息。例如,您可以使用此方法
显示在编辑时应该可见的重叠视图。

This method notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing.

还有一个属性:

@property(nonatomic, readonly, getter=isEditing) BOOL editing

Apple文档:


一个布尔值,字段当前处于编辑
模式。 (只读)

A Boolean value indicating whether the text field is currently in edit mode. (read-only)

这篇关于如何以编程方式关闭键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆