0

In my create profile I have only two text fields: weight and date of birth. When the user touches for weight the keyboard shows. But when the user touches the date of birth a date picker appears in the action sheet. When the user selects the date and press the done button the action sheet disappears but the keyboard remains open. And there is no way to hide this keyboard. I have used resignFirstResponder method but no luck.

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
Ravi Nalawade
  • 11
  • 1
  • 5
  • 2
    What have you tried? Post some code. `resignFirstResponder` is the right way to go about it. – visakh7 Dec 20 '11 at 06:58
  • Click the correct answer as correct by clicking the arrow. It will help you in future to get constant help from SO. Thanks. – Yama Dec 20 '11 at 12:19

4 Answers4

4

you need to do this when you want to hide the keyboard:

[textfield resignFirstResponder];
visakh7
  • 26,380
  • 8
  • 55
  • 69
Shanti K
  • 2,873
  • 1
  • 16
  • 31
  • Are you sure that you are resigning the correct textfield? It may be that the textfield variable is nil, or points to a different text field to the one that's selected. Try logging the value of textfield using NSLogo prior to calling resignFirstResponder on it. – Nick Lockwood Dec 20 '11 at 08:36
  • Oh I was forgot [txtField becomesFirstResponder] – Ravi Nalawade Dec 20 '11 at 12:17
1

Did you include the method :

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

or

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([txtComment isFirstResponder] && [touch view] != txtComment)
    {
        [txtComment resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}
Yama
  • 2,649
  • 3
  • 31
  • 63
1

[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; will work for you.

Andrey Zverev
  • 4,409
  • 1
  • 28
  • 34
0
-(void) ViewDidLoad
{

 // your some codes

   UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
   [self.view addGestureRecognizer:gestureRecognizer];
   gestureRecognizer.cancelsTouchesInView = NO;
}

- (void) hideKeyboard 
{
    [textfiledname1 resignFirstResponder];
    [textfieldname2 resignFirstResponder];
}
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41