在文本字段外面隐藏键盘 [英] Hide keyboard on touch outside of textfield
问题描述
我试图在屏幕上的任何其他地方触摸后隐藏键盘。我正在使用的代码基于此答案。
I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.
IBOutlet UITextView *myTextView;
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
我不明白我应该如何将我的UITextField链接到 touchesBegan
方法。我需要使用哪个发送的事件?此外,该方法不应该是IBAction,因为现在我无法将我的UITextField连接到它。
What I don't understand is how I should link my UITextField to the touchesBegan
method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
我还给了这段代码试一试但是那个却打破了我的导航按钮(即使是在提到的解决方案中)评论)
I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)
推荐答案
我应该如何将我的UITextField链接到touchesBegan方法。我需要使用哪个发送的事件?此外,该方法不应该是IBAction,因为现在我无法将我的UITextField连接到它。
how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
因为你不喜欢T。您必须在文本字段为子视图的视图上覆盖此方法。
Because you don't. You have to override this method on the view of which the text field is a subview.
这篇关于在文本字段外面隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!