Dismiss The Keyboard - Multiple UITextFields in iOS 7

送分小仙女□ 提交于 2019-12-03 04:34:58

问题


below you'll find my .h & .m files for my primary viewcontroller.

I have 3 questions.

1.) Because I have multiple uitextfields, do I have to set each with their own resignFirstResponder statement ? and 2.) where would I do that, in what method ? 3.) Is my syntax right for resigning the first responder ?

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.

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

.h file

@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

#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;
}

回答1:


Resigning the textField: All your textField.delegate should be set as ViewController's object. 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:

//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;



回答2:


Try the following:

[[self view] endEditing:YES]



回答3:


  -(BOOL) textFieldShouldReturn: (UITextField *) textField{

        [textField resignFirstResponder];
        return YES;
  }

also connect your UITextField Delegate.




回答4:


This answer works for iOS 7 and arc,

  1. dismiss keyboard when user touches return: in ViewController add the following action

    -(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.

  1. dismiss keyboard when user touches background: implement the following method in the 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];
        }
    



回答5:


Here's what I use in my code. It works great and is more efficient than the other answers.

In yourviewcontroller.h add:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

Now in the .m file, add this to your ViewDidLoad function:

- (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:

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



回答6:


When the UITextField in question calls the delegate method of - (BOOL)textFieldShouldReturn:(UITextField*)textField it passes itself in as the argument.

So the specific textField available as an argument to this method IS the specific one you care about. Within this delegate method, you can just refer to it as "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's suggestion to verify that the delegate is indeed assigned is critical as well. 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. Otherwise the delegate message will go nowhere and promptly be ignored!




回答7:


try to use self.dancePlace.delegate = self; instead of dancePlace.delegate = self; to set the UITextFieldDelegate. Does that work?




回答8:


In swift 3 :

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


来源:https://stackoverflow.com/questions/19647096/dismiss-the-keyboard-multiple-uitextfields-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!