how can I dismiss keyboard when I will just touch anywhere except UITextField?

后端 未结 10 1999
独厮守ぢ
独厮守ぢ 2021-02-19 11:12

Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?

10条回答
  •  死守一世寂寞
    2021-02-19 11:43

    Use delegate,

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
    }
    

    You can also create a method in your controller

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

    and then in Connection Inspector in IB connect Event "Did End On Exit" to it.

提交回复
热议问题