xcode 当我触摸键盘外部时 UIKeyboard 不会隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9077528/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:09:52  来源:igfitidea点击:

UIKeyboard does not hide when I touch outside the keyboard

objective-ciosxcodeuikeyboard

提问by user784625

I'm not figuering out a way to hide the keyboard when I touch outside the keyboard, is there any event that could hide the keyboard if I click outside the keyboard. Thanks

当我在键盘外触摸时,我没有想出隐藏键盘的方法,如果我在键盘外单击,是否有任何事件可以隐藏键盘。谢谢

回答by Tendulkar

In the ViewController.m write the touches began method.In that method write the resignFirstResponder

在 ViewController.m 中编写 touches started 方法。在该方法中编写 resignFirstResponder

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

}

回答by Yas T.

Here is a better solution that is gonna work for any UITextField in your view controller. You can even add it to your base view controller (if you have got one) and it will work like a charm.

这是一个更好的解决方案,适用于您的视图控制器中的任何 UITextField。你甚至可以将它添加到你的基本视图控制器(如果你有的话),它会像魅力一样工作。

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

    UITouch *touch = [[event allTouches] anyObject];

    if (![[touch view] isKindOfClass:[UITextField class]]) {
        [self.view endEditing:YES];
    }
    [super touchesBegan:touches withEvent:event];
}

回答by Simon Lee

No, you need to do that yourself. The normal approach is to place a transparent button over the screen (but below your text field) and then on the action for the button...

不,你需要自己做。通常的方法是在屏幕上(但在文本字段下方)放置一个透明按钮,然后在按钮的操作上...

- (void)dismissPressed:(id)sender {
  [myTextField resignFirstResponder];
}