How to remove added done button from numeric keyboard - iphone

This is my code....
`
- ( void ) registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- ( void ) keyboardWasShown:(NSNotification*)notification {
[self addButtonToKeyboard];
}
#pragma -
#pragma Numeric keyboard done button
- ( void ) keyboardDoneClicked:(id)sender {
[txtvannum resignFirstResponder];
[txtmobnum resignFirstResponder];
// [sender resignFirstResponder];
}
- ( void ) addButtonToKeyboard {
// create custom button
// if(keyButton){
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(keyboardDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
// }
//// else{
//// return;}
}
`
I developed an app which consist of user to enter their details.There are 4 text fields in that.Two among those requires numeric type of keyboard.For that numeric keyboard I added the done button to resign after finished editing.But that done button is coming for all other type of keyboard also.How can add that done button oly to numeric keyboard so that it should hide for all other type.I am struggling in this.Please help.
Thank you.

For each UITextField use the function setReturnKeyType: which can attain values,
typedef enum {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;
And you have to set the key type for each text field separately.For the text field that you don't want the done button just set it as UIReturnKeyDefault.
Hope this helps.

First of all, you have to set a notification to keep tracking keyboard's activity. To identify, which UITextField is being active, you have to set an unique tag to each.
In .h file >>
#interface myViewController : UIViewController
{
UITextField *txtField1; // Allows Numeric input
UITextField *txtField2; // Allows String input
UITextField *txtField3; // Allows Numeric input
UITextField *txtField4; // Allows String input
UITextField *txtFieldTemp; // Temp textfield
BOOL isReturned;
}
In .m file >>
- (void)viewDidLoad
{
txtField1.tag = 0;
txtField2.tag = 1;
txtField3.tag = 2;
txtField4.tag = 3;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(UIKeyboardWillChangeFrameNotification:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)UIKeyboardWillChangeFrameNotification:(NSNotification *)note
{
if (txtFieldTemp.tag == 0 || txtFieldTemp.tag == 2)
{
[self addButtonToKeyboard];
}
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag == 0 || textField.tag == 2)
{
[self addButtonToKeyboard];
}
else
{
[self removeButtonFromKeyboard];
}
txtFieldTemp = textField;
}
- (void)removeButtonFromKeyboard
{
// Remove Keyboard logic
}

Related

How set a keyboard for number text field and alphabetic text field?

I have a layout where i have two text fields one is for alphabetic input and another one for numeric input. I have set different type of keyboards for these like as default for alphabetic text field and number for numeric field.
I have added a done button on number pad. Still I am getting this a issue with keyboards that is when I taped on numeric text filed first then it show done button keyboard while I taped alphabetic text filed first and then taped on numeric text field then it doesn't add done button on number pad.
How do I solve it?
I have use these code to add button on number pad.
-(void)viewWillAppear:(BOOL)animated
{
// Register for the events
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
keyboardVisible = NO;
scroll_view.contentSize=CGSizeMake(320, 400);
}
- (void)keyboardDidShow:(NSNotification *)note {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for (keyboard in tempWindow.subviews) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
if (numberPadShowing) {
[self addButtonToKeyboard];
return;
break;
} else {
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
-
(void) keyboardDidHide: (NSNotification *)notif
{
// Is the keyboard already shown
if (!keyboardVisible) {
return;
}
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(void)doneButton
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[myActiveTextField resignFirstResponder];
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
myActiveTextField=textField;
if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){
numberPadShowing=TRUE;
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
svos = scrollView.contentOffset;
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrollView];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scrollView setContentOffset:pt animated:YES];
}
else{
numberPadShowing=FALSE;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[textField resignFirstResponder];
return YES;
}
Thanks in advances...
I think your problem is becuase you are tapping in to the numeric field after tapping the alpha field, and the alpha keyboard is already in view. Therefore when you tap the number field (whilst alpha keyboard still in view) it doesn't fire the 'keyboardDidShow' event, as the keyboard is already showing from the previous field (albeit a alpha one).
Try putting your done button logic into a delegate method for the textfield 'textFieldDidBeginEditing' event for the number field.
If my first answer doesn't work in your implementation, then you might want to consider a restructure and go for setting the inputAccessoryView of your UITextField. Code along these lines should do the trick (you can put this in your viewDidLoad nib, and also note that you'll need to implement the doneTapped method yourself);
// Create a UIToolbar object and set its style to be black and transparent
numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent;
// The flexible space item will push the done button to the far right
UIBarButtonItem *space = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself.
UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTapped:)];
// Stick the flexible space and the done button in your toolbar
numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil];
// This would return it, if you had this in a separate method.
return numericInputAccessoryView_;

How to hide numeric keypad?

I have a textfield, in which I need the user to type only numeric values. I am using the numeric keypad type. In this keyboard there no done button. I saw an article here
which allowed me to add a done button to the keypad.
I can't hide that, however. Here is my code
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)doneButton:(id)sender {
NSLog(#"doneButton");
[anualIncome setText:#"hai"];
NSLog(#"Input: %#", anualIncome.text);
[anualIncome resignFirstResponder];
}
kindly guide me where i'm doing wrong.when i press the done button i can see the console output like this
Hello World[2542:f803] doneButton
Hello World[2542:f803] Input: (null)
i just add my anualincome in to the my veiwconroller.is there any other action should add?this is my outlet
#property(nonatomic, retain) IBOutlet UITextField *anualIncome;
You may use an inputAccessoryView property of the UITextField instance to set a UIToolbar with any buttons on it. (in fact it can be any UIView subclass) The resulting view will be as it is in the safari, when you filling some text in any web form. It's much easier. Also there's no chance of breaking view in case apple will change their implementation...
Happy coding...
Without using done button you can hide keyboard this way:
Implement touches began method in your viewContoller class like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[anualIncome resignFirstResponder];
}

How to add a done button to the keyboard in landscape mode?

I have an application in which I have a text field. When I click on that the screen scrolls and the keyboard is shown. I use this code:
- (void) viewWillAppear:(BOOL)animated {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
[self willAnimateRotationToInterfaceOrientation:orientation duration:0];
[super viewWillAppear:animated];
NSLog(#"Registering for keyboard events");
// Register for the events
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
// Setup content size
scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT);
//Initially the keyboard is hidden
keyboardVisible = NO;
}
- (void)keyboardWillShow:(NSNotification *)note {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for (keyboard in tempWindow.subviews) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
if (numberPadShowing) {
[self addButtonToKeyboard];
return;
break;
} else {
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(#"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible)
{
NSLog(#"Keyboard is already visible. Ignore notification.");
return;
}
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = scrollView.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = scrollView.frame;
viewFrame.size.height -= keyboardSize.height;
scrollView.frame = viewFrame;
UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
CGRect textFieldRect = [myActiveTextField frame];
textFieldRect.origin.y += 20;
[scrollView scrollRectToVisible:textFieldRect animated:YES];
} else {
CGRect textFieldRect = [myActiveTextField frame];
textFieldRect.origin.y += 80;
[scrollView scrollRectToVisible:textFieldRect animated:YES];
}
NSLog(#"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(#"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
scrollView.frame = CGRectMake(0, 44, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
scrollView.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
doneButton.frame = CGRectMake(0, 260, 160, 40);
} else {
doneButton.frame = CGRectMake(0, 163, 106, 53);
}
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
Now the problem is that it works fine in portrait mode, but in landscape mode when I click on the textfield the keyboard will appear and the view will scroll so much that the text field being edited is no longer visible on the screen. Also on a number keyboard ( like the one found in Phone.app ) there is no custom add button in landscape mode, but there is one in portrait mode.
How do I fix this?
I use this code for add done button in landscape mode
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIInterfaceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
doneButton.frame = CGRectMake(0, 123, 160, 39);
}
else{
doneButton.frame = CGRectMake(0, 163, 106, 53);
}
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
this method is for both mode for number pad.

Adding Done Button on numberpad Keyboard

I am having one registration form which are having five textfield in which one textfield is phonenumber textfield and we take keyboard for that uikeyboardtypenumberpad, but no done button on numberpadkeyboard. I am adding done button but button is added for all textfields but I want only for phonenumber field.
My code is:
- (void)viewDidLoad
{
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonAction)];
self.navigationItem.leftBarButtonItem = leftButton;
// UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc]initWithTitle:#"About" style:UIBarButtonItemStyleBordered target:self action:#selector(AboutButtonAction)];
// self.navigationItem.rightBarButtonItem = rightbarButton;
[super viewDidLoad];
NSLog(#"Events Id is : %#", particularEventId);
phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(56, 223, 220, 31)];
phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
phoneNumber.returnKeyType = UIReturnKeyDone;
phoneNumber.textAlignment = UITextAlignmentLeft;
[self.view addSubview:phoneNumber];
// add observer for the respective notifications (depending on the os version)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if(keyboard == phoneNumber)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)doneButton:(id)sender {
NSLog(#"doneButton");
NSLog(#"Input: %#", phoneNumber.text);
[phoneNumber resignFirstResponder];
}
I used this code for my project, I think it isn't optimized but it works very well.
.h
BOOL firstTime;
BOOL add;
BOOL keyboardOpened;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
firstTime = TRUE;
add = TRUE;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
doneButton.tag = 3;
[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 found, add the button
if ([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES && add) [keyboard addSubview:doneButton];
}
}
- (void)removeButtonFromKeyboard
{
// 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 found, remove the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES) [[keyboard viewWithTag:3] removeFromSuperview];
}
}
- (void)doneButton:(id)sender {
[firstResponder resignFirstResponder];
if (![[[UIDevice currentDevice] model] isEqualToString:#"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:#"iPad Simulator"])
{
[self removeButtonFromKeyboard];
firstTime = TRUE;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
firstResponder = textField;
for (int i = 0; i < [[[profile operationObject] keys] count]; i++)
{
if ([[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:1] isEqual:textField])
{
editingPath = [NSIndexPath indexPathForRow:i inSection:0];
break;
}
}
if (![[[UIDevice currentDevice] model] isEqualToString:#"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:#"iPad Simulator"])
{
if(!firstTime)
{
if(textField.keyboardType == UIKeyboardTypeNumberPad)
{
add = TRUE;
[self addButtonToKeyboard];
}
else
{
add = FALSE;
}
}
else
{
firstTime = FALSE;
if(textField.keyboardType != UIKeyboardTypeNumberPad)
{
add = FALSE;
}
}
keyboardOpened = TRUE;
}
}
- (void)keyboardDidShow:(id)sender
{
if (![[[UIDevice currentDevice] model] isEqualToString:#"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:#"iPad Simulator"])
{
NSLog(#"%#",[[UIDevice currentDevice] model]);
[self addButtonToKeyboard];
keyboardOpened = TRUE;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[[[profile operationObject] values] replaceObjectAtIndex:[editingPath row] withObject:[textField text]];
firstResponder = nil;
if (![[[UIDevice currentDevice] model] isEqualToString:#"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:#"iPad Simulator"])
{
[self removeButtonFromKeyboard];
keyboardOpened = FALSE;
}
}

Disapperiang IPhone Numeric Keyboard

How I can disappear numeric keyboard in iPhone because there is no return key in it, which implies it is not going to respond to
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Use resignFirstResponder in touchesBegan, like so:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[self.myTextField resignFirstResponder];
}
}
You may need to call this on multiple text fields if you're not sure where the focus is currently located; I haven't tested that case.
Additionally, in Interface Builder, you'll need to enable the "User Interaction Enabled" checkbox for the view, or programatically use:
myView.userInteractionEnabled = YES;
Here's the answer..
-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
{
//[URL resignFirstResponder];
//[Password resignFirstResponder];
[speedField resignFirstResponder];
[super touchesBegan:touches withEvent:event ];
}
Try this:
[theTextField resignFirstResponder];
Why not to use "Numbers and Punctuation" keyboard instead of "Numeric"? It will save your time
Here you go. Extend your UIViewController Class with this. But how do we know it is the done button? Adding Done Button to Only Number Pad Keyboard on iPhone
//
// UIViewController+NumPadReturn.m
// iGenerateRandomNumbers
//
// Created by on 12/4/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "UIViewController+NumPadReturn.h"
#implementation UIViewController (NumPadReturn)
-(void) viewDidLoad{
// add observer for the respective notifications (depending on the os version)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[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 found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)doneButton:(id)sender {
NSLog(#"doneButton");
[self.view endEditing:TRUE];
}