mrd
mrd

Reputation: 4699

How to dismiss keyboard when an UIControl is touched?

This has been asked a few times, but I don't seem to get it.

I have a view controller, with two textfields, one segmented control, a date picker and a few labels.

I want to dismiss the keyboard, when the user clicks the background or the segmented control or the date picker. Here is my .h file:

@interface MRPatientenViewController : UIViewController
@property (nonatomic, strong) MRPatientenTableViewController *delegate;
- (IBAction)textFieldReturn:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *nachnameTextField;
@property (strong, nonatomic) IBOutlet UITextField *vornameTextField;
@property (strong, nonatomic) IBOutlet UIDatePicker *geburtsdatumPicker;
@property (strong, nonatomic) IBOutlet UISegmentedControl *genderSegmentedControl;
@end

this is my .m file:

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

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

Now this dismisses the keyboard, if the background or the labels are touched.

But how do I dismiss the keyboard if the UISegmentedControl or the datepicker is touched?

Upvotes: 0

Views: 1055

Answers (5)

mrd
mrd

Reputation: 4699

This is the solution which worked for me: the MRViewController.h file:

#import <UIKit/UIKit.h>
@interface MRViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@end

The MRViewController.m file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self hideKeyboardWhenBackgroundIsTapped];
    self.textField.delegate = self;
}
-(void)hideKeyboardWhenBackgroundIsTapped{
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [tgr setCancelsTouchesInView:NO];
    [self.view addGestureRecognizer:tgr];
}
-(void)hideKeyboard{
    [self.view endEditing:YES];
}

And in my storyboard scene, I got a UITextField and a UISegmentedControl.

Upvotes: 0

Ajay
Ajay

Reputation: 1622

You can do the following:

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
tapGesture.delegate=self;
[self.view addGestureRecognizer:tapGesture];

- (void)tapAction
{
    [nachnameTextField resignFirstResponder];
    [vornameTextField resignFirstResponder];
}

and for UISegmentedControl or UIDatePicker you probably have their own selector methods for example: For UISegmentedControl :

- (IBAction)mainSegmentControl:(UISegmentedControl *)segment{
}

or For UIDatePicker :

- (void)changeDate:(UIDatePicker *)sender {
}

just call your method [self tapAction]; in both above methods and you are good to go...!!

Upvotes: 0

Kirit Modi
Kirit Modi

Reputation: 23407

You Touch code is correct but it also working fine in my code.when you click outside of text-field at that time Touch event call but its inner condition is not working good.i remove the condition and check its working fine.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [nachnameTextField resignFirstResponder];
    [super touchesBegan:touches withEvent:event];
}

enter image description here

Upvotes: 3

Ayaz Alavi
Ayaz Alavi

Reputation: 4835

Have you tried following line of code

[textfieldInFocus resignFirstResponder];

Upvotes: 0

Aravind G S
Aravind G S

Reputation: 392

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[geburtsdatumPicker addGestureRecognizer:tap];
[genderSegmentedControl addGestureRecognizer:tap];
[self.view addGestureRecognizer:tap];

Then resign keyboard inside the selector

-(void) dismissKeyboard
{
    [nachnameTextField resignFirstResponder];
    [vornameTextField resignFirstResponder];
}

Upvotes: 1

Related Questions