Dismiss The Keyboard – Multiple UITextFields in iOS 7

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

I have 3 questions.

  • Swift 3 : tableView duplicating loaded images from Firebase
  • How to set lock screen , wallpaper and Ringtone programmatically in iPhone?
  • Xcode 6.0.1 Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
  • Type 'Any' has no subscript members after updating to Swift 3
  • Add a UIView above all, even the navigation bar
  • iOS clickable text inside UITextView
  • 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 ()
    @implementation ViewController
    - (void)viewDidLoad
        [super viewDidLoad];
        [self retrieveFromParse];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        // 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;

    8 Solutions Collect From Internet About “Dismiss The Keyboard – Multiple UITextFields in iOS 7”

    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;

    Try the following:

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

    also connect your UITextField Delegate.

    This answer works for iOS 7 and arc,

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

          [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];

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

    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!

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

    In swift 3 :

     //Dismiss keyboard , When touch outside
     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)