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

Chatar Veer Suthar picture Chatar Veer Suthar · Feb 9, 2011 · Viewed 9.1k times · Source

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?

Answer

Ishu picture Ishu · Feb 9, 2011

use tochesBegin method for this purpose

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

  [yourFirstTextField resignFirstResponder];
  [yourSecondTextField resignFirstResponder];
}

You do not need any thing else.When you touch anywhere on view then both textField resign no need to to know which one having focus.and when you touch textField then textField open keyboard by own.