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];
}
Related
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
}
I am using a text field to enter mobile nuber and I would like to add done button to hide the keyboard, following is my code
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
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 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];
}
}
}
but I am unable to add the done button ,after cross check with break points , I observe that control is not entering into the if([[keyboard description] hasPrefix:#"UIKeyboard"] == YES) conditon.
I am using IOS5.
I did add a custom done-button in one of my projects too. The tutorial I used mentioned that piece of code:
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
if([[tmpKeyboard description] hasPrefix:#"<UIPeripheralHost"] == TRUE)
[tmpKeyboard addSubview:doneButton];
}
else
{
if([[tmpKeyboard description] hasPrefix:#"<UIKeyboard"] == TRUE)
[tmpKeyboard addSubview:doneButton];
}
Prior to iOS version 3.2 your approach with UIKeyboard is fine, but later you have to change it to UIPeripheralHost.
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];
}
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.
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;
}
}