触摸键盘辞职 [英] Resigning keyboard on touch

查看:106
本文介绍了触摸键盘辞职的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些代码可以帮助我在用户触摸UITextView元素的屏幕时重新键盘。

I've found some code that helps me resign the keyboard when a user touches the screen off of the UITextView element.

以下是它的外观:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([self.speechBubble.speechText isFirstResponder] && [touch view] != self.speechBubble.speechText){
    [self.speechBubble.speechText resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}

到目前为止,此功能完美无缺,如果用户触及任何地方,将会移除键盘在文本视图之外。但是,它只适用于我正在运行它的特定对象,所以如果我有两个speechBubbles,它将无法工作。

This works perfectly so far, and will remove the keyboard if a user touches anywhere outside of the text view. However, it only works for the particular object that I'm running it for, so if I have two speechBubbles, it won't work.

如何更改此这样任何speechBubble会有同样的效果吗? (我可以将此代码从我的ViewController移动到我的SpeechBubble类,但我对如何使用[touch view]在speechBubble视图之外进行触摸有一点问题。)谢谢

How can I change this so that ANY speechBubble will have the same effect? (I could move this code from my ViewController to my SpeechBubble class, but I'd have a little issue with how to use [touch view] to get touches outside of the speechBubble's view. ) Thanks

推荐答案

我刚刚发现的一些可能对您有用的东西是:

Something I just discovered recently that may be of use to you is:

[self.view endEditing:YES];

它将从当前拥有它的任何元素中辞退第一响应者,而无需手动跟踪它你自己。

It will resign first responder from any element that currently has it without you having to manually keep track of it yourself.

参考你的示例代码,这样的事情可能有用,这取决于你的speechBubbles如何工作:

In reference to your example code, something like this might work, depending on how your speechBubbles work:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [[event allTouches] anyObject];
  // Note the '!':
  if(![[touch view] class] isKindOfClass [speechBubble class]]){
    // It's not a bubble they touched, dismiss the keyboard:
    [self.view endEditing:YES];
  }
  [super touchesBegan:touches withEvent:event];
}

这篇关于触摸键盘辞职的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆