1
votes

I have an application which has a couple of UITextField present to allow my user to enter their age, and another numerical value. Ideally, I want the keyboard to bring up the numeric keypad when the TextField is being edited. At present I have it set to Numer and Punctuation merely to make use of the 'Done' button to dismiss the keyboard as the Numeric pad does not have a done button.

In an attempt to use the Numeric keypad, I have tried to set it to dismiss by tapping the background of my main view.

-(IBAction)backgroundTapped:(id)sender;

I created the above action in my header file.

-(IBAction)backgroundTapped:(id)sender {
    [ageEntry resignFirstResponder];
}

I have expanded on the above method in my implementation file to tell the ageEntry TextField to resignFirstResponder. I have also changed my main view to a UIControl class and connected the buttonTapped action to the relevant alert through Interface Builder. Yet when I touch the background nothing happens.

Any ideas?

3
you want to add done button into numeric keyboard ?Maulik
Well ideally id like a done button in the numeric keypad, but that isnt in the options. I want to be able to tap my background to dismiss the keypadPaul Morris

3 Answers

1
votes

Just detect the touch in your viewController's view using the method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [urAgeField resignFirstResponder];
    [urOtherField resignFirstResponder];
}

and resign the keyboard in this method

1
votes

A much easier method is to add a inputAccessoryView to your text field. This input accessory view can be a UIToolbar with a single UITabBarButton for your Done button.

Much less of a hack, and will look like the accessory view that is used in for example Safari to dismiss the keyboard.

0
votes

i had a similar problem. here is the solution..

EDITED

add

- (void)viewDidLoad 
{
        [super viewDidLoad];
         UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                    action:@selector(tap:)];
        tap.numberOfTapsRequired = 1;

        [self addGestureRecognizer:tap];


}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   [ageEntry resignFirstResponder];            
}