I have a DetailViewController, which implementation file contains this code:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController <UITextFieldDelegate>
{
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *numberField;
}
@end
In my storyboard, I have set the ViewController‘s to DetailViewController and connected the delegate of both of my UITextFields to my DetailViewController. The implementation file of my DetailViewController contains this method to dismiss the keyboard when tapping somewhere other than the text field:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
This method is not called though, I have tested this using a breakpoint. What could be going wrong?
rdelmar is correct, the code you have only gets triggered when the user hits the “return” key on the keyboard, not when they click outside of the keyboard.
To get the behavior you are looking for, I’d add a Tap Gesture Recognizer to the view behind your text field, then put
[nameField resignFirstResponder];and[numberField resignFirstResponder];in the tap gesture recognizer’s code.