How to dismiss keyboard when user tap other area outside textfield?

today I tried to run my code on my iPod (iOS 6.1.3) and I found something interesting here...

first, when I tap on textfield the keyboard shows up but it won't hide when I tap somewhere else outside textfield.

so I decided to Googling and found this solution :

_fieldEmail.delegate = self;
_fieldEmail.returnKeyType = UIReturnKeyDone;

_fieldPassword.delegate = self;
_fieldPassword.returnKeyType = UIReturnKeyDone;

_fieldRegisterName.delegate = self;
_fieldRegisterName.returnKeyType = UIReturnKeyDone;

_fieldRegisterEmail.delegate = self;
_fieldRegisterEmail.returnKeyType = UIReturnKeyDone;

_fieldRegisterPassword.delegate = self;
_fieldRegisterPassword.returnKeyType = UIReturnKeyDone;

it works... it gives a 'DONE' button on the bottom of keyboard and now the keyboard can be hidden by pressing it.

but I have 2 problems here :

  1. the keyboard only hide when 'DONE' button is tapped. not by tapping other area outside text field. I don't know if this normal on iOS world, but usually I see lot of apps don't act like this.
  2. is there any way to loop this process so I don't have manually add that delegate one by one of all textfield that I have? how to do that?

that's all I need to know


Solution 1:

The below code will work on all the components in the UIView for all the UITextField

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView * txt in self.view.subviews){
        if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
            [txt resignFirstResponder];
        }
    }
}

OR

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];    
}

Solution 2:

  1. Simply add an UITapGestureRecogniser to your view that will lead to calling resignFirstResponder

In viewDidLoad :

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] 
                                   initWithTarget:self
                                   action:@selector(hideKeyBoard)];

[self.view addGestureRecognizer:tapGesture];

And then :

-(void)hideKeyBoard {
       [yourTextField resignFirstResponder];
}

2.You could subclass UITextField but unless you have 1000 textFields it is ok to do like you currently do.

Solution 3:

This is regards to Swift programming for Xcode 6.0.1

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
    tapRecognizer.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(tapRecognizer)
}

func handleSingleTap(recognizer: UITapGestureRecognizer) {
    self.view.endEditing(true)
}

Solution 4:

Use Either

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];

and code of method

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [self.TextFiledName resignFirstResponder];

}

OR _ And The best Other option is

Just add

  [self.view endEditing:YES];

And key board will hide when you tapped anywhere from view:)

Solution 5:

Dismissing the keyboard

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES];
        [super touchesBegan:touches withEvent:event];
    }