Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss The Keyboard - Multiple UITextFields in iOS 7

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;
}
like image 748
Miles Works Avatar asked Oct 28 '13 23:10

Miles Works


5 Answers

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];
        }
    
like image 141
Daniel Shalev Avatar answered Oct 28 '22 04:10

Daniel Shalev


Try the following:

[[self view] endEditing:YES]
like image 40
Kamaros Avatar answered Oct 28 '22 05:10

Kamaros


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;
like image 40
dRAGONAIR Avatar answered Oct 28 '22 03:10

dRAGONAIR


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

        [textField resignFirstResponder];
        return YES;
  }

also connect your UITextField Delegate.

like image 4
Mirko Catalano Avatar answered Oct 28 '22 05:10

Mirko Catalano


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];
}
like image 4
Takide Avatar answered Oct 28 '22 04:10

Takide