I'm trying to get rid of the keyboard when the user touch outside my UITextField, by using this method:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[mainTextController resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
However, this seems to call a method that is called after pressing the return button on the keyboard, but I just want the keyboard to vanish, not to press return for me. How can I accomplish that?
Thanks!
EDIT: tGilani's answer is the most straight-forward way, works like a charm, without changing to UIControl. But I guess jonkroll's answer also works.
try
Update:
Take a boolean value and set it to false in init method. In your
textFieldShouldReturn
delegate method method, execute the code if it is false, skip otherwisein your method where you call the endEditing method, set boolean to true.
You need to change your
ViewController's
view property fromUIView
toUIControl
using the Identity Inspector:From there, you simply create an
IBAction
and tell the textfield to dismiss (which I am assuming is yourmainTextController
). IfmainTextController
is not the textfield you want the keyboard to dismiss on then change theresignFirstReaponder
method to your textfield like so.then from there go back into your View Contoller's
.xib
file and connect the action to the Control View and select "Touch Down".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:
Next, in your storyboard scene for your
ViewController
(ornib
, if you are not using storyboards) change the class of your ViewController'sview
property fromUIView
toUIControl
. Theview
property is effectively the background behind your other UI elements. The class type needs to be changed becauseUIView
cannot respond to touch events, butUIControl
(which is a direct subclass ofUIView
) can respond to them.Finally, in your ViewController's
viewDidLoad:
method, tell your view controller to execute yourdismissKeyboard
method when the view receives aUIControlEventTouchDown
event.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 intextFieldDidEndEditing:
. 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:
and then call that method via Target-Action when your text field sends the control event
UIControlEventEditingDidEndOnExit
.Note that
UIControlEventEditingDidEndOnExit
is different thanUIControlEventEditingDidEnd
. 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.