当我只是触摸UITextField以外的任何地方时,如何解除键盘?

Cha*_*har 13 iphone keyboard uitextfield numeric-keypad

你好我的应用程序中有两个UITextFields,当我只是触摸UITextFields以外的任何地方时想要解除键盘我该怎么办?

Ish*_*shu 24

为此目的使用tochesBegin方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

  [yourFirstTextField resignFirstResponder];
  [yourSecondTextField resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

你不需要任何其他东西.当你触摸视图中的任何地方时,两个textField都不需要知道哪个有焦点.当你触摸textField然后textField打开键盘自己.


Mad*_*dav 11

Ishu的答案很好.但我认为你也应该尝试:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

  [self.view endEditing:YES];
}
Run Code Online (Sandbox Code Playgroud)