ios 隐藏键盘ios
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10389476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hiding keyboard ios
提问by Builder Brent
I have a few text inputs and I can hide the keyboard whenever I touch the background, but only when I have been entering into the first text box name textField1. now this code should be simple but I just can't seem to get it.
我有一些文本输入,每当我触摸背景时我都可以隐藏键盘,但只有当我输入第一个文本框名称 textField1 时。现在这段代码应该很简单,但我似乎无法理解。
-(IBAction)backgroundTouched:(id)sender {
[textField1 resignFirstResponder];
[buildLength resignFirstResponder];
[buildWidth resignFirstResponder];
[ridgeWidth resignFirstResponder];
[rafterWidth resignFirstResponder];
[hipWidth resignFirstResponder];
[eaveOverhang resignFirstResponder];
[spacing resignFirstResponder];
}
回答by Bhavin
If you want to hide the keyboard when you tap a button and you have more than one UITextFields
in your view
, then you should use:
如果要隐藏键盘,当你点击一个按钮,你有多个UITextFields
在你view
,那么你应该使用:
[self.view endEditing:YES];
Tap anywhere on the view, and the keyboard will disappear.
点击视图上的任意位置,键盘将消失。
回答by user2248428
Try this:
尝试这个:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self view] endEditing:YES];
}
回答by Ospho
You can also iterate through an array of views (such as your UIView's subviews) and manually resign the keyboard, this is good if you dont want to resign on ALL the subviews within your parent UIView.
您还可以遍历一组视图(例如 UIView 的子视图)并手动退出键盘,如果您不想退出父 UIView 中的所有子视图,这很好。
- (void)viewDidLoad
{
self.view.userInteractionEnabled = TRUE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Iterate through your subviews, or some other custom array of views
for (UIView *view in self.view.subviews)
[view resignFirstResponder];
}
回答by Sonu
You can try UITouch
method, and in this set your text field object and call resignFirstResponder
when ever you touch on the screen the keyboard will resign, I hope this will work for you.
您可以尝试UITouch
方法,并在此设置您的文本字段对象并resignFirstResponder
在您触摸屏幕时调用键盘将退出,我希望这对您有用。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentSelectedTextField resignFirstResponder];
}