0

In my app I have a view that is a form that has quite a few inputs. When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.

If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:

  • calling resignFirstResponder in textFieldDidEndEditing
  • calling [self.view endEditing:YES] in textFieldDidEndEditing
  • calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.

Any ideas would be great.

I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour. http://dl.dropbox.com/u/61692457/KB_Test.zip

pasawaya
  • 11,515
  • 7
  • 53
  • 92
GrantC
  • 81
  • 6
  • possible duplicate of [iPad keyboard will not dismiss if modal view controller presentation style is UIModalPresentationFormSheet](http://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui) – Matthias Bauch Jun 29 '12 at 02:53

2 Answers2

2

Declare a Global UITextField in .h file

UITextField *txtfld;

Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code

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

if (textField.tag == 1 || textField.tag==3)
{

    if(numPickerPopover == nil)
    {   

        numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];

        numPicker.delegate = self;

        numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
        [numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
    }

    [numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    [txtfld resignFirstResponder];
    return NO;
}


if (textField.tag == 2)

{

    txtfld = textField;

    return YES;
}

return YES;    

}
Bhupesh
  • 2,310
  • 13
  • 33
  • Thank you for this. I took the idea and simplified it and it works a treat. Not sure why my way didn't work... – GrantC Jun 29 '12 at 04:42
0

To dismiss the keyboard when the user touches the textField that brought it up, add this method:

- (IBAction)dismissKeyboard:(id)sender {

    [textField resignFirstResponder];
}

In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).

pasawaya
  • 11,515
  • 7
  • 53
  • 92
anuragbh
  • 603
  • 1
  • 5
  • 11
  • I want the keyboard to disappear when the user touches another UITextField (not the same Text Field). I gave it a shot anyway but I am not using the Interface Builder, I added: `[tf3 addTarget:self action:@selector(dismissKeyboard) forControlEvents: UIControlEventTouchUpInside];)` but that did not fire the event. – GrantC Jun 29 '12 at 04:11
  • It should be easier if you want it dismissed when another TextField is touched as you can just link that textfield to the method that dismisses the keyboard in the IB. I am not sure of the non-IB way to link the 'Touch Up Inside' Event to the method. Try NSLog to make sure event is fired, maybe it is but the keyboard isn't dismissed. – anuragbh Jun 29 '12 at 04:19
  • Thats what I have done using UITextFieldDelegate with textFieldDidBeginEditing and textFieldDidEndEditing. But it didn't work....... – GrantC Jun 29 '12 at 04:28