hiding keyboard ios [duplicate]
Asked Answered
G

4

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];
}
Gregoire answered 30/4, 2012 at 19:34 Comment(2)
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.Intercellular
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).Indian
L
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.

Loose answered 21/12, 2012 at 14:13 Comment(1)
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.Weise
B
28

Try this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
     [[self view] endEditing:YES];
}
Bedder answered 5/4, 2013 at 12:9 Comment(0)
M
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];
}
Mollee answered 31/3, 2013 at 7:0 Comment(0)
F
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];
}
Furlana answered 1/5, 2012 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.