Understanding resignFirstResponder with UITextField

后端 未结 3 802
情深已故
情深已故 2021-02-06 11:23

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:(UIEven         


        
相关标签:
3条回答
  • 2021-02-06 11:45

    try

    [self.view endEditing:YES];
    

    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 otherwise

    - (BOOL) textFieldShouldReturn:(UITextField*)textField
    {
        if (!boolean)
        {
            // YOur code logic here
        }
        boolean = false;
    }
    

    in your method where you call the endEditing method, set boolean to true.

    boolean = YES;      
    [self.view endEditing:YES];
    
    0 讨论(0)
  • 2021-02-06 11:48

    You need to change your ViewController's view property from UIView to UIControl using the Identity Inspector:

    From there, you simply create an IBAction and tell the textfield to dismiss (which I am assuming is your mainTextController). If mainTextController is not the textfield you want the keyboard to dismiss on then change the resignFirstReaponder method to your textfield like so.

    - (IBAction)backgroundTap:(id)sender {
    [myTextField resignFirstResponder];
    }
    

    then from there go back into your View Contoller's .xib file and connect the action to the Control View and select "Touch Down".

    0 讨论(0)
  • 2021-02-06 11:50

    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.

    0 讨论(0)
提交回复
热议问题