用MULTIPLE UITextFields消除键盘? [英] Dismiss the keyboard with MULTIPLE UITextFields?
问题描述
当您有多个UITextFields时,是否可以关闭键盘?如果可以,怎么办?
Is it possible to dismiss the keyboard when you have MULTIPLE UITextFields ? If so how ?
请注意,我是否必须关闭每个"字段的键盘,还是可以全局使用它?哦,如果我不必触摸完成"按钮,那就太酷了,理想情况下,我希望找到一种解决方案,使用户触摸任何东西,但所涉及的字段却自动消失……
As a side note, do I have to dismiss the keyboard for Each and Every field or can it be done globally ? Oh and it would be super cool if I don't have to touch the DONE button, I'd ideally like a solution that where the user touches anything BUT the field in question and the keyboard automagically disappears...
哦,如果您愿意,请按部就班.
Oh and if you'd be so kind step by step instructions.
我应该补充一点,我已经有一种方法可以让键盘辞职....
I should have added that I have a method already to resign the keyboard....
但是,它仅在提交我的表单时运行! (请参见下面的方法)
However, it only runs when my form is submitted! (see method below)
我的问题是如何隐藏/关闭键盘而不必跳过那么多该死的篮球!您会发现6年后,成熟的操作系统将有办法在全球范围内隐藏键盘....不!
My question is how to the keyboard to hide/dismiss without having to jump thru so many damned hoops! You'd figure after 6 years, a mature operating system would have a way to GLOBALLY hide the keyboard....NOT!
好吧,足够的抱怨....
Ok, enough whining....
- (void)hideKeyboard {
[self.dancePlace resignFirstResponder];
[self.danceGate resignFirstResponder];
[self.danceTerminal resignFirstResponder];
[self.danceText resignFirstResponder];
[self.danceDate resignFirstResponder];
[self.danceStyle resignFirstResponder];
[self.danceTimeOut resignFirstResponder];
}
提交我的按钮时就会调用它....
And this is called when my button is submitted....
- (IBAction)addListingPressed:(id)sender {
// NSLog(@"BUTTON PRESSED");
[self hideKeyboard];
[self valuesAdded];
}
我的问题是,假设任何人都可以回答这个问题……而我怀疑不是,如果满足以下条件,是否有办法全局隐藏键盘:1.)用户从任何现有字段中点击OUT, 2.)按屏幕上的其他任何地方. 3.)在现有的viewcontroller.m文件中最多不超过一两行. 4.)我不必在viewcontroller上添加一个令人困惑的按钮. (任何时候我必须添加插座,该死的东西都在我身上崩溃……然后发生恶作剧,的确……记住我只是个初学者,读起来很困惑我必须在这里和那里放置这个……好吧,简单的人,简单的.我不是在寻找优雅的解决方案,只是为了使它可行.
My question, assuming anyone can answer this...and I suspect not, is there a way to globally hide the keyboard if the following conditions are MET: 1.) the user taps OUT of any one of the existing fields, 2.) presses anywhere else on the screen. 3.) Is no more than a line or two in the existing viewcontroller.m file. 4.) I don't have to add a confusing button on the viewcontroller. (any time I have to add outlets, the damned thing is crashing on me...and then nastiness happens, and really...remember I am JUST a beginner, and its very confusing to read that I have to place this here and that there...oy. Simple folks, simple. I'm not looking for elegant solution, just so that it works.
推荐答案
我有一个超类,我的所有视图控制器都继承自该超类.在那个班上,我有这段代码.
I have a super class that all my view controllers inherit from. In that class I have this code.
MySuperViewController.h
MySuperViewController.h
#import <UIKit/UIKit.h>
@interface MySuperViewController : UIViewController
@property(strong, nonatomic) UITapGestureRecognizer *backgroundTapGestureRecognizer;
@end
MySuperViewController.m
MySuperViewController.m
- (void)viewDidLoad{
//add a tap gesture recognizer to capture all tap events
//this will include tap events when a user clicks off of a textfield
self.backgroundTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onBackgroundTap:)];
self.backgroundTapGestureRecognizer.numberOfTapsRequired = 1;
self.backgroundTapGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:self.backgroundTapGestureRecognizer];
}
- (void)onBackgroundTap:(id)sender{
//when the tap gesture recognizer gets an event, it calls endEditing on the view controller's view
//this should dismiss the keyboard
[[self view] endEditing:YES];
}
我将UITapGestureRecognizer作为公共属性,因此可以在需要时覆盖它.
I have the UITapGestureRecognizer as a public property, so I can override it if I need to.
子类
MyViewController.h
MyViewController.h
#import <UIKit/UIKit.h>
#import "MySuperViewController.h"
@interface MyViewController : MySuperViewController<UIGestureRecognizerDelegate>
@end
MyViewController.m
MyViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//You don't always want the keyboard to be dismissed, so you tie into the gesture recognizer's delegate method
//By doing this, you can stop the endEditing call from being made
[self.backgroundTapGestureRecognizer setDelegate:self];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//touch.view is the view that recieved the touch
//if this view is another textfield or maybe a button, you can return NO and the endEditing call won't be made
if (touch.view == self.myViewThatShouldNotBeBlocked) {
return NO;
}
//if you want the gesture recognizer to accept the event, return yest
return YES;
}
我将示例项目上传到github. https://github.com/JeffRegan/KeyboardBeGone
I uploaded an example project to github. https://github.com/JeffRegan/KeyboardBeGone
这篇关于用MULTIPLE UITextFields消除键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!