11

I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.

How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.

progrmr
  • 75,956
  • 16
  • 112
  • 147
jaydev
  • 1,629
  • 5
  • 17
  • 31

5 Answers5

11

You can do this in this way:

@property (nonatomic, strong) UITextField *textField;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonDidPressed:)];
    UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
    [toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
    self.textField.inputAccessoryView = toolbar;
}

- (void)doneButtonDidPressed:(id)sender {
    [self.textField resignFirstResponder];
}

+ (CGFloat)toolbarHeight {
    // This method will handle the case that the height of toolbar may change in future iOS.
    return 44.f;
}
Oscar
  • 651
  • 5
  • 13
  • 2
    For iOS 6.1, this did not work unless I initialized the toolbar with a frame like so: `UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];` Otherwise, perfect answer – Segfault Apr 22 '13 at 19:24
  • @Segfault Confirming that. You need to specify a frame in order for this to work correctly in 6.1. – Paul Jun 22 '13 at 12:40
7

you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation, It'll not only show the return key but also provide you with some extra punctuations

Najeebullah Shah
  • 4,164
  • 4
  • 35
  • 49
1

There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -

if (there is a touch outside of your textField)
{

   [textField resignFirstResponder];

}
Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • but i want to create a done button on number pad ,so how to create this done button on number pad . if it is posible then provide help me. – jaydev May 01 '11 at 06:42
  • sorry but that's not possible with iPhone sdk with using private APIs' you have to go with some alternative solutions, You can create your own keyboard with done button on it.. – Saurabh May 01 '11 at 09:23
0

This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];

If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.

What you do want to do to simulate a return key is to actually add it like this:

Register for a keyboard did show notification and add this to the code:

if ([self.passCodeLabel isFirstResponder])
        {
            UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            doneButton.frame = CGRectMake(0, 163, 106, 53);
            //doneButton.frame = CGRectMake(0, 163, 257, 257);
            doneButton.adjustsImageWhenHighlighted = NO;
            [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
            [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
            [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

            // locate keyboard view
            UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
            NSLog(@"%@",[[UIApplication sharedApplication] windows]);

            UIView* keyboard;
            NSLog(@"Shared applicaiton windows count:%i",tempWindow.subviews.count);
            for(int i=0; i<[tempWindow.subviews count]; i++) {
                keyboard = [tempWindow.subviews objectAtIndex:i];
                NSLog(@"%@",[keyboard description]);
                // keyboard view found; add the custom button to it
                if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
                {
                    NSLog(@"Adding return button");
                    [keyboard addSubview:doneButton];
                }
            }
        } 

This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).

Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.

  • Create the button

  • Look for the keyboard subview

  • Add the button to that subview

Pochi
  • 13,391
  • 3
  • 64
  • 104
0
- (void)viewDidLoad
{

    [super viewDidLoad];
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];

    _txt_mobileno.inputAccessoryView = numberToolbar;

}

-(void)doneWithNumberPad
{
    [_txt_mobileno resignFirstResponder];

}
Paresh Hirpara
  • 487
  • 3
  • 10