用户点击文本域外的其他区域时如何关闭键盘? [英] How to dismiss keyboard when user tap other area outside textfield?
问题描述
今天我尝试在我的iPod(iOS 6.1.3)上运行我的代码,我在这里找到了一些有趣的东西...
today I tried to run my code on my iPod (iOS 6.1.3) and I found something interesting here...
首先,当我点击文本字段时当我点击文本字段外的其他地方时,键盘会显示但不会隐藏。
first, when I tap on textfield the keyboard shows up but it won't hide when I tap somewhere else outside textfield.
因此我决定使用Google搜索并找到此解决方案:
so I decided to Googling and found this solution :
_fieldEmail.delegate = self;
_fieldEmail.returnKeyType = UIReturnKeyDone;
_fieldPassword.delegate = self;
_fieldPassword.returnKeyType = UIReturnKeyDone;
_fieldRegisterName.delegate = self;
_fieldRegisterName.returnKeyType = UIReturnKeyDone;
_fieldRegisterEmail.delegate = self;
_fieldRegisterEmail.returnKeyType = UIReturnKeyDone;
_fieldRegisterPassword.delegate = self;
_fieldRegisterPassword.returnKeyType = UIReturnKeyDone;
它有效...它在键盘底部提供了一个DONE按钮,现在是键盘可以通过按下来隐藏。
it works... it gives a 'DONE' button on the bottom of keyboard and now the keyboard can be hidden by pressing it.
但我在这里有2个问题:
but I have 2 problems here :
- 键盘仅在点击完成按钮时隐藏。不要通过点击文本字段外的其他区域。我不知道iOS世界是否正常,但通常我看到许多应用程序都不是这样的。
- 有没有办法循环这个过程所以我不这样做有人手动添加该委托我所拥有的所有文本字段?怎么做?
这就是我需要知道的全部
that's all I need to know
推荐答案
以下代码适用于UIView中所有 UITextField
The below code will work on all the components in the UIView for all the UITextField
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
这篇关于用户点击文本域外的其他区域时如何关闭键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!