Here's how I've handled this before. First create a method on your view controller that will dismiss the keyboard by resigning first responder status on your text field:
- (IBAction)dismissKeyboard:(id)sender
{
[mainTextController resignFirstResponder];
}
Next, in your storyboard scene for your ViewController
(or nib
, if you are not using storyboards) change the class of your ViewController's view
property from UIView
to UIControl
. The view
property is effectively the background behind your other UI elements. The class type needs to be changed because UIView
cannot respond to touch events, but UIControl
(which is a direct subclass of UIView
) can respond to them.
Finally, in your ViewController's viewDidLoad:
method, tell your view controller to execute your dismissKeyboard
method when the view receives a UIControlEventTouchDown
event.
- (void)viewDidLoad
{
[super viewDidLoad];
UIControl *viewControl = (UIControl*)self.view;
[viewControl addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchDown];
}
EDIT:
Part of your concern seems to be that textFieldDidEndEditing:
is called when the keyboard is dismissed. That is unavoidable, it will always be called whenever a text field loses focus (i.e. first responder status). It sounds like your problem is that you have put code to perform when the user clicks the return button in textFieldDidEndEditing:
. If you do not want that code to run when the user touches outside of the text field, that is not the proper place to put it.
Instead, I would put that code in a separate method:
- (IBAction)textFieldReturn:(id)sender
{
if ([mainTextController isFirstResponder]) {
[mainTextController resignFirstResponder];
// put code to run after return key pressed here...
}
}
}
and then call that method via Target-Action when your text field sends the control event UIControlEventEditingDidEndOnExit
.
[mainTextController addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
Note that UIControlEventEditingDidEndOnExit
is different than UIControlEventEditingDidEnd
. The former is called when editing ends by the user touching outside the control, the latter is called when editing ends by the user pressing the return key.