Set the delegate to UITextField
, then implement UITextField delegate methods
this will enable the user to use the Keyboards Return Key to hide the keyboard.
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
If you are using a UIButton then
- (IBAction)hideKeyboard {
[self.view endEditing:YES];
}
If you want to resign the keyboard when the user touch the screen outside the UITextField then use the following code which will work on all the UITextField
in the UIView
- (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];
}