Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>- (IBAction)dismissKeyboard:(id)sender { [mainTextController resignFirstResponder]; } </code></pre> <p>Next, in your storyboard scene for your <code>ViewController</code> (or <code>nib</code>, if you are not using storyboards) change the class of your ViewController's <code>view</code> property from <code>UIView</code> to <code>UIControl</code>. The <code>view</code> property is effectively the background behind your other UI elements. The class type needs to be changed because <code>UIView</code> cannot respond to touch events, but <code>UIControl</code> (which is a direct subclass of <code>UIView</code>) can respond to them.</p> <p>Finally, in your ViewController's <code>viewDidLoad:</code> method, tell your view controller to execute your <code>dismissKeyboard</code> method when the view receives a <code>UIControlEventTouchDown</code> event.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; UIControl *viewControl = (UIControl*)self.view; [viewControl addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchDown]; } </code></pre> <p>EDIT:</p> <p>Part of your concern seems to be that <code>textFieldDidEndEditing:</code> 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 <code>textFieldDidEndEditing:</code>. 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.</p> <p>Instead, I would put that code in a separate method:</p> <pre><code>- (IBAction)textFieldReturn:(id)sender { if ([mainTextController isFirstResponder]) { [mainTextController resignFirstResponder]; // put code to run after return key pressed here... } } } </code></pre> <p>and then call that method via Target-Action when your text field sends the control event <code>UIControlEventEditingDidEndOnExit</code>.</p> <pre><code>[mainTextController addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit]; </code></pre> <p>Note that <code>UIControlEventEditingDidEndOnExit</code> is different than <code>UIControlEventEditingDidEnd</code>. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload