简体   繁体   English

关闭键盘 - iOS 7中的多个UITextFields

[英]Dismiss The Keyboard - Multiple UITextFields in iOS 7

below you'll find my .h & .m files for my primary viewcontroller. 在下面你会找到我的主视图控制器的.h和.m文件。

I have 3 questions. 我有3个问题。

1.) Because I have multiple uitextfields, do I have to set each with their own resignFirstResponder statement ? 1.)因为我有多个uitextfields,我是否必须使用自己的resignFirstResponder语句设置每个字段? and 2.) where would I do that, in what method ? 2.)我会用哪种方法做到这一点? 3.) Is my syntax right for resigning the first responder ? 3.)我的语法是否适合辞职第一响应者?

Also it would be really nice if I could dismiss the keyboard when the user clicks out of the field NOT on hitting the return key! 如果我可以在用户点击字段时关闭键盘而不是点击返回键,那将是非常好的!

I know this has been asked and answered before, but to be honest with you i'm still a little confused as to what goes where. 我知道之前有人问过并回答过这个问题,但老实说,我仍然有点困惑。

I'm using storyboards, with XCode 5, and iOS 7. 我正在使用故事板,XCode 5和iOS 7。

============================= =============================

.h file .h文件

@interface ViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *danceDate;
@property (weak, nonatomic) IBOutlet UITextField *dancePlace;
@property (weak, nonatomic) IBOutlet UITextField *danceTerminal;
@property (weak, nonatomic) IBOutlet UITextField *danceGate;

.m file .m文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self retrieveFromParse];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // SET DELEGATE HERE
    //
    // if I uncomment 1 of these lines, i'll get an error.
    //
    // _dancePlace.delegate = self; 
    // dancePlace.delegate = self; 
    // dancePlace = self; 

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}

-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    [textField resignFirstResponder];

    return YES;
}


-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    return YES;
}

请尝试以下方法:

[[self view] endEditing:YES]

Resigning the textField: All your textField.delegate should be set as ViewController's object. 重新启动textField:所有textField.delegate都应设置为ViewController的对象。 And then implement the below delegate method. 然后实现下面的委托方法。

-(BOOL) textFieldShouldReturn: (UITextField *) textField {
[textField resignFirstResponder];
    return YES;
}

To dismiss Keyboard on tap of the View: Add a Tap gesture to your ViewController.view as follows: 要点击View,请关闭键盘:向ViewController.view添加点按手势,如下所示:

//declare a property to store your current responder
@property (nonatomic, assign) id currentResponder;


//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];

//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)iSender {
    [self.currentResponder resignFirstResponder];
}
// was missing ; after the call --> [self.currentResponder resignFirstResponder]
    // also in textFieldDidEndEditing set self.currentResponder = nil;
  -(BOOL) textFieldShouldReturn: (UITextField *) textField{

        [textField resignFirstResponder];
        return YES;
  }

also connect your UITextField Delegate . 还连接您的UITextField 代理

This answer works for iOS 7 and arc, 这个答案适用于iOS 7和arc,

  1. dismiss keyboard when user touches return: in ViewController add the following action 用户触摸返回时关闭键盘:在ViewController中添加以下操作

     -(IBAction)textFieldReturn:(id)sender { [sender resignFirstResponder]; } 

next, in main.storyboard select the textField and from the connections inspector control + drag "Did End On Exit" event to the view controller. 接下来,在main.storyboard中选择textField并从连接检查器控件+将“End End On Exit”事件拖到视图控制器。

  1. dismiss keyboard when user touches background: implement the following method in the ViewController 用户触摸背景时关闭键盘:在ViewController中实现以下方法

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

Here's what I use in my code. 这是我在代码中使用的内容。 It works great and is more efficient than the other answers. 它工作得很好,比其他答案更有效。

In yourviewcontroller.h add: 在yourviewcontroller.h中添加:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

Now in the .m file, add this to your ViewDidLoad function: 现在在.m文件中,将其添加到ViewDidLoad函数中:

- (void)viewDidLoad {
    //Keyboard stuff
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapRecognizer];
}

Also, add this function in the .m file: 另外,在.m文件中添加此函数:

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
    [self.view endEditing:YES];
}

When the UITextField in question calls the delegate method of - (BOOL)textFieldShouldReturn:(UITextField*)textField it passes itself in as the argument. 当有问题的UITextField调用委托方法- (BOOL)textFieldShouldReturn:(UITextField*)textField它将自己作为参数传递。

So the specific textField available as an argument to this method IS the specific one you care about. 因此,作为此方法的参数可用的特定textField是您关心的特定textField Within this delegate method, you can just refer to it as "textField". 在此委托方法中,您可以将其称为“textField”。

That means that you should use what Mirko Catalano advised calling resignFirstResponder on textField rather than on the individual properties like you were doing. 这意味着您应该使用Mirko Catalano建议在textField上调用resignFirstResponder而不是像您正在执行的单个属性。

Mirko's suggestion to verify that the delegate is indeed assigned is critical as well. Mirko建议验证代表确实被分配也是至关重要的。 You'll want to make sure that ALL of your UITextFields in the nib or storyboard have their delegate property pointing to File's Owner. 您需要确保nib或storyboard中的所有UITextField都具有指向File的所有者的委托属性。 Otherwise the delegate message will go nowhere and promptly be ignored! 否则委托消息将无处可及,并立即被忽略!

try to use self.dancePlace.delegate = self; 尝试使用self.dancePlace.delegate = self; instead of dancePlace.delegate = self; 而不是dancePlace.delegate = self; to set the UITextFieldDelegate. 设置UITextFieldDelegate。 Does that work? 那样有用吗?

In swift 3 : 在快速3:

 //Dismiss keyboard , When touch outside
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        self.view.endEditing(true)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM