隐藏键盘ios

我有几个文本input,我可以隐藏键盘,只要我触摸背景,但只有当我已经进入第一个文本框名称textField1。 现在这个代码应该是简单的,但我似乎无法得到它,我做错了什么?

-(IBAction)backgroundTouched:(id)sender { [textField1 resignFirstResponder]; [buildLength resignFirstResponder]; [buildWidth resignFirstResponder]; [ridgeWidth resignFirstResponder]; [rafterWidth resignFirstResponder]; [hipWidth resignFirstResponder]; [eaveOverhang resignFirstResponder]; [spacing resignFirstResponder]; 

}

如果您想在按下button时隐藏键盘,并且在view有多个UITextFields ,则应该使用:

 [self.view endEditing:YES]; 

点击视图中的任何位置,键盘将消失。

尝试这个:

 - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } 

你也可以遍历视图数组(例如你的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]; } 

你可以尝试UITouch方法,并在此设置你的文本字段对象,并呼吁resignFirstResponder当你触摸屏幕上的键盘将辞职,我希望这会为你工作。

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [currentSelectedTextField resignFirstResponder]; }