1

I'm developing an iphone app.

When my app running (during test on my computer), i need to change the value in a textfield box but when it s done, the keyboard stays, and i can't get back to the app, i'm trapped !

How could i close the keyboard and go on ?

Many thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2971617
  • 63
  • 1
  • 9
  • Please search first: http://stackoverflow.com/questions/12227859/hide-keyboard-on-touch-outside-of-textfield – trojanfoe Jan 19 '14 at 10:30
  • Cos it's been asked before: http://stackoverflow.com/questions/3338967/hiding-the-keyboard-when-uitextfield-loses-focus – trojanfoe Jan 19 '14 at 10:30

1 Answers1

1

Set the delegate to UITextField, then implement UITextField delegate methods this will enable the user to use the Keyboards Return Key to hide the keyboard.

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

If you are using a UIButton then

- (IBAction)hideKeyboard {
    [self.view endEditing:YES];
}

If you want to resign the keyboard when the user touch the screen outside the UITextField then use the following code which will work on all the UITextField in the UIView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView * txt in self.view.subviews){
        if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
            [txt resignFirstResponder];
        }
    }
}

OR

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];    
}
icodebuster
  • 8,890
  • 7
  • 62
  • 65
  • @user2971617 If you find the answer useful please accept it as right answer which will help to remove this question from unanswered question list. – icodebuster Jan 22 '14 at 07:48