42

I have a few text inputs and I can hide the keyboard whenever I touch the background, but only when I have been entering into the first text box name textField1. now this code should be simple but I just can't seem to get it.

-(IBAction)backgroundTouched:(id)sender {
    [textField1 resignFirstResponder];
    [buildLength resignFirstResponder];
    [buildWidth resignFirstResponder];
    [ridgeWidth resignFirstResponder];
    [rafterWidth resignFirstResponder];
    [hipWidth resignFirstResponder];
    [eaveOverhang resignFirstResponder];
    [spacing resignFirstResponder];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Builder Brent
  • 455
  • 1
  • 5
  • 8
  • What object is receiving the `backgroundTouched:` action? Is it the view? Some object you put behind everything? The way I got a keyboard to hide on iOS is to override the ViewController's `touchesEnded:withEvent:` . It gets called when no other objects are able to handle a touch event. In there is where I resign the first responder, though you need to check `isFirstResponder` because if you don't consume the touch you are suppose to call super. – Russ Apr 30 '12 at 19:42
  • Agree with @Russ... touch events on the view controller is the simpler way to go. But it's still mysterious if it works for textField1. Why not the others? My guess would be that the other handles are no good (e.g. that 'buildLength' is not properly initialized). – danh Apr 30 '12 at 19:50

4 Answers4

190

If you want to hide the keyboard when you tap a button and you have more than one UITextFields in your view, then you should use:

[self.view endEditing:YES];

Tap anywhere on the view, and the keyboard will disappear.

Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • I noticed, that in some cases this method works after some delay about 0.3 sec maximum. I mean, resignFirstResponder removes keyboard immediately. But this method not and you should use performSelector after delay sometimes after using this. – Denis Kutlubaev Apr 22 '13 at 14:54
28

Try this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
     [[self view] endEditing:YES];
}
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
user2248428
  • 291
  • 3
  • 4
7

You can also iterate through an array of views (such as your UIView's subviews) and manually resign the keyboard, this is good if you dont want to resign on ALL the subviews within your parent UIView.

- (void)viewDidLoad
{
    self.view.userInteractionEnabled = TRUE;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //Iterate through your subviews, or some other custom array of views
    for (UIView *view in self.view.subviews)
        [view resignFirstResponder];
}
Ospho
  • 2,756
  • 5
  • 26
  • 39
2

You can try UITouch method, and in this set your text field object and call resignFirstResponder when ever you touch on the screen the keyboard will resign, I hope this will work for you.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{  
    [currentSelectedTextField resignFirstResponder];
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
Sonu
  • 248
  • 1
  • 10