我有几个文本输入,每当我触摸背景时我都可以隐藏键盘,但只有在我输入第一个文本框时才会命名为textField1。现在这段代码应该很简单,但我似乎无法得到它。
-(IBAction)backgroundTouched:(id)sender {
[textField1 resignFirstResponder];
[buildLength resignFirstResponder];
[buildWidth resignFirstResponder];
[ridgeWidth resignFirstResponder];
[rafterWidth resignFirstResponder];
[hipWidth resignFirstResponder];
[eaveOverhang resignFirstResponder];
[spacing resignFirstResponder];
}
答案 0 :(得分:189)
如果您想在点按按钮时隐藏键盘,并且UITextFields
中有多个view
,则应使用:
[self.view endEditing:YES];
点按视图上的任意位置,键盘将消失。
答案 1 :(得分:27)
试试这个:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self view] endEditing:YES];
}
答案 2 :(得分:6)
您还可以遍历一系列视图(例如您的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];
}
答案 3 :(得分:2)
您可以尝试使用UITouch
方法,并在此设置文本字段对象并调用resignFirstResponder
当你触摸屏幕键盘将会辞职,我希望这对你有用。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentSelectedTextField resignFirstResponder];
}