问题描述
如何通过按回车键隐藏键盘
有几件事需要记住。开发人员忘记设置的#1部分是textField的委托。
如果使用的是Interface Builder,则必须记住您需要将textField的委托设置为文件Owner。
如果不使用Interface Builder,请确保将文本字段的委托设置为self。我还包括returnType。例如,如果textField被称为gameField:
gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;
还必须为ViewController实现 UITextFieldDelegate 。
@interface YourViewController:UIViewController< UITextFieldDelegate>
最后,您需要使用 textFieldShouldReturn 方法并调用 [ textField resignFirstResponder]
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder ];
返回是;
}
所有textField都将使用相同的方法,因此您只需要进行此设置一旦。只要为textField设置了委托,就为该接口实现了UITextFieldDelegate,则可以添加textFieldShouldReturn方法并调用
resignFirstResponder作为您的集合。
How to Hide Keyboard by pressing Returnkey
There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.
If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.
alt text http://www.thoughtblog.com/imgs/delegate.png
If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:
gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;
You must also implement the UITextFieldDelegate for your ViewController.
@interface YourViewController : UIViewController <UITextFieldDelegate>
Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]
-(BOOL) textFieldShouldReturn:(UITextField*) textField {
[textField resignFirstResponder];
return YES;
}
All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call theresignFirstResponder your set.
这篇关于禁用键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!