4

I have a UIDatePickerView inside UIActionSheet as a input to the UITextField. When focusing on UITextField UIActionSheet will popup instead of Keyboard. When clicking on the done button in the UIActionSheet it'll hide. I have several other text fields behave as normal (Showing keyboard).

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([textField isEqual:txtExpDate]) {
        [textField resignFirstResponder];
        [self showDatePicker];
    }
}


- (void) showDatePicker{
    UIActionSheet *datePickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Done" otherButtonTitles:nil];
    datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 80, 0, 0)];
    datePickerView.datePickerMode = UIDatePickerModeDate;
   [datePickerActionSheet addSubview:datePickerView];
   [datePickerActionSheet showInView:self.navigationController.view];
   [datePickerActionSheet setBounds:CGRectMake(0, 0, 320, 500)];
   [datePickerActionSheet release];

}

Now my problem is, Let say first user taps on normal textfield. Which will popups keyboard. Then without selecting done button he taps on date field. Which will popup action sheet (without dismissing the keyboard). After hiding actionsheet user has to tap on other text filed and click on return key of the keyboard.

I want to hide the keyboard if action sheet is going to popup?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178

6 Answers6

7

Make the text field resignFirstResponder when you are going to bring up the action sheet. If you have more than one text fields, create an instance variable of type UITextField in .h file.

UITextField *currentTextField;

Then you can keep reference of the current text field in textFieldDidBeginEditing: method.

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    currentTextField = textField;
    ....

And, call [currentTextField resignFirstResponder] in showDatePicker method.

- (void)showDatePicker {

    [currentTextField resignFirstResponder];
    UIActionSheet *datePickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Done" otherButtonTitles:nil];
    ...

EDIT: @Samuel Goodwin is correct. We don't have to track the text fields. We can simply do the following to dismiss the keyboard.

- (void)showDatePicker {

    [self.view endEditing:YES];
    ...
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
3

If you want to dismiss the keyboard for any reason, simply do:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) target:nil forEvent:nil];

Worked for all text fields everywhere anywhere.

Samuel Goodwin
  • 1,698
  • 13
  • 17
  • I tried the other tips but this one is working like a charm. Just a short notice, on my side the function is not with the parameter "target" but with [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; – tryp Nov 16 '16 at 14:33
0
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:txtExpDate]) {

        [self showDatePicker];
        return NO;
    }
    return YES;

}
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
iKushal
  • 2,689
  • 24
  • 20
0

I know this is a very old post but I just thought I'd add the solution that worked for me (the others didn't). I found that I had to re-assign a text field to be first responder the resign it again. It doesn't matter which text field becomes then looses first responder status, this is just a way to ditch that keyboard.

[self.textfield becomeFirstResponder];
[self.textfield resignFirstResponder];
0

It is working fine.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(textField==myTextField2){
        [myTextField1 resignFirstResponder];
        [self showActionSheet];
        return NO;
    }
    return YES;      
}
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
0

The method proposed by EmptyStack did not work for me (using xcode 4.2). I placed the message [currentTextField resignFirstResponder] in the method willPresentActionSheet:(UIActionSheet *)actionSheet, that also didn't work. Then I put the message in actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex and that did the trick... strange!

stema
  • 90,351
  • 20
  • 107
  • 135
staccata
  • 664
  • 1
  • 7
  • 17