关闭 UITextField 上的键盘
我是一个为 iOS 设备开发的新手。我在 InterfaceBuilder 上插入了一个 UITextField,并分配了代码:
@interface ComposeViewController : UIViewController {
id <ComposeViewControllerDelegate> delegate;
IBOutlet UITextField *notificationTitle;
}
How I could allow to close the keyboard when the user press the "Return" key?I'm a newbie developing for iOS devices. I inserted an UITextField on InterfaceBuilder, and I assigned with the code:
@interface ComposeViewController : UIViewController {
id <ComposeViewControllerDelegate> delegate;
IBOutlet UITextField *notificationTitle;
}
How I could allow to close the keyboard when the user press the "Return" key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 UITextField 的 Delegate 设置为 ViewController,在 File's Owner 和 UITextField 之间添加引用出口,然后实现此方法:
Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:
继承UITextFieldDelegate协议
在viewDidLoad方法中设置:
Implement the delegate method below:
Inherit UITextFieldDelegate protocol
In viewDidLoad method set:
Implement the delegate method below:
继承
UITextFieldDelegate
协议并实现textFieldShouldReturn:
,即可捕获“return”事件。在
textFieldShouldReturn
内写入[notificationTitle resignFirstResponder];
Inherit
UITextFieldDelegate
protocol and implementtextFieldShouldReturn:
, that's you will catch "return" event.Inside
textFieldShouldReturn
write[notificationTitle resignFirstResponder];
将操作目标添加到事件 Did End on Exit(UIControlEventEditingDidEndOnExit),在目标函数中使用 resignFirstResponder 从提交的文本中删除第一响应者。
添加操作目标
注意:
1. Nib --- 在退出时对 Did End 进行操作
2. 在代码中将目标操作添加到事件 UIControlEventEditingDidEndOnExit。
Add a action target to the event Did End on Exit(UIControlEventEditingDidEndOnExit), in the target function remove the first responder from the text filed using resignFirstResponder.
Adding action target
Note:
1. Nib --- give action to even Did End on Exit
2. In code add target action to the event UIControlEventEditingDidEndOnExit.