How to resign first responder from text field when user tap elsewhere?

前端 未结 18 3114
野趣味
野趣味 2020-12-14 02:12

I have filled my view with ScrollView (same size as the view) and I\'m stuck at how to resign first responder when user tap elsewhere in the View (or the scrollview). Any id

相关标签:
18条回答
  • 2020-12-14 02:28

    The best option is the shortest way ;)

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2020-12-14 02:35

    Nobody has yet presented the best answer here:

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    

    From the documentation on this method:

    Normally, this method is invoked by a UIControl object that the user has touched. The default implementation dispatches the action method to the given target object or, if no target is specified, to the first responder. Subclasses may override this method to perform special dispatching of action messages.

    I bolded the relevant part of that paragraph. By specifying nil for the to: parameter, we automatically call the given selector on the first responder, wherever it is, and we don't need to have any knowledge of where in the view hierarchy it is. This can also be used to invoke other methods on the first responder as well, not just cause it to resign first responder status.

    0 讨论(0)
  • 2020-12-14 02:36

    I found an answer here: link

    If your ultimate aim is just to resign the first responder, this should work: [self.view endEditing:YES]

    This method is designed right for it! Reference: endEditing:

    Causes the view (or one of its embedded text fields) to resign the first responder status.

    0 讨论(0)
  • 2020-12-14 02:39

    For Swift 3 and Swift 4 you can do this:

    func viewDidLoad() {
        super.viewDidLoad()
    
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboardByTappingOutside))
    
        self.view.addGestureRecognizer(tap)
    }
    
    @objc func hideKeyboardByTappingOutside() {
        self.view.endEditing(true)
    }
    
    0 讨论(0)
  • 2020-12-14 02:39

    Create your IBOutlet in ViewController.h file like below:

    //YOUR ViewController.h FILE
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    {
        //WIRE THIS WITH YOUR UITextField OBJECT AT YOUR .xib FILE
        IBOutlet UITextField *yourIBOutletUITextField;
    }
    
    @end
    

    Add this to your ViewController.m file:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         [yourIBOutletUITextField resignFirstResponder];
    }
    

    like below:

    //YOUR ViewController.m FILE
    
    #import "ViewController.h"
    
    @implementation ViewController
    
    
    //ADD THIS METHOD BELOW
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         [yourIBOutletUITextField resignFirstResponder];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-14 02:42

    Give a unique Tag to your UITextfield (i.e. 333), then to resign the first responder do this:

    UITextField *textField = (UITextField *)[self.view viewWithTag:333];
    [textField resignFirstResponder];
    
    0 讨论(0)
提交回复
热议问题