Thursday, July 1, 2010

iPhone:Add done button to number pad in iphone

Here! We will be knowing how to add 'Done' button on number pad in iphone. this is bit tricky..
Problem specification:
In IB where we can select keyboard type I have changed it to number pad and this code doesn't seem to work for me.

- (BOOL)textFieldShouldReturn:(UITextField *)textBoxName {
[textBoxName resignFirstResponder];
return YES;
Type: Number Pad
Return Key: Done

And Auto-enable done key is checked.

But there is no done button appears on number pad!!!!
Hopes are alive ..................
I got the solution :)

when you will see the number pad on iphone ,there is a blank space on left bottom.This is the place which I will use to put Done Image,and use it to get this number pad disappear ...
First add these two images to your resorce folder.

-(void)keyboardWillShow:(NSNotification *)note {

// create custom button

UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
// exactly of image size
doneButton.frame = CGRectMake(0, 163, 106, 53);

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];

UIView* keyboard;

for(int i=0; i<[tempWindow.subviews count]; i++) {

keyboard = [tempWindow.subviews objectAtIndex:i];

// keyboard view found; add the custom button to it

if([[keyboard description] hasPrefix:@"
[keyboard addSubview:doneButton];



//// declare it in .h

-(IBAction) doneButton:(id) sender

//objNumberPad is the Keyboard
[objNumericPad resignFirstResponder];



- (void)viewDidLoad {

//Notification to add done buton on num pad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];


- (void)viewDidUnload {

[[NSNotificationCenter defaultCenter] removeObserver:self];


I'm using it in live application,Hope this will resolve your problem.

No comments:

Post a Comment