Understanding resignFirstResponder with UITextField

后端 未结 3 801
情深已故
情深已故 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: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".

提交回复
热议问题