I'm adding the Done button to the Num pad using NSNotifications keyboard observer.
It however does not always work in all cases:
1. User click on the DONE buttons for either pad to close that pad each time they move from field to field, then it works 100%.
2. User clicks on the next field without first clicking on the DONE button, then the NSNotification will not observe any keyboard changes and so the keyboard observers will not work.
I would appreciate anyone who has a solution to this as it will help a lot of people struggling with the Done button added to the num pad.
I am attaching the complete code. It will help if you can review that and see what can be done. If we can get working, then I will post the code for all.
Thanks ahead!
__________________ iPhone Apps: Who SAid™, iFRICA™, iFA™, iReceipt™,eCash™
I'm adding the Done button to the Num pad using NSNotifications keyboard observer.
It however does not always work in all cases:
1. User click on the DONE buttons for either pad to close that pad each time they move from field to field, then it works 100%.
2. User clicks on the next field without first clicking on the DONE button, then the NSNotification will not observe any keyboard changes and so the keyboard observers will not work and the DONE button display persists on default pad.
I would appreciate anyone who has a solution to this as it will help a lot of people struggling with the Done button added to the num pad.
I am attaching the complete code. It will help if you can review that and see what can be done. If we can get working, then I will post the code for all.
Thanks ahead!
Here is the ViewController code:
Code:
@implementation DoneViewController
@synthesize mynumberField,mytextField,addDone,doneButton;
- (void)viewDidLoad
{
[super viewDidLoad];
//Add observers 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];
}
}
#pragma mark -
#pragma mark DONE button for Numeric Keypad
- (void)keyboardWillShow: (NSNotification *)notification {
//Num pad - add Num Pad
NSLog(@"addDone: %@\n", (addDone ? @"YES" : @"NO"));
if (addDone == YES) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0];
}
}
}
- (void)keyboardDidShow:(NSNotification *)notification {
//Num pad - add Num Pad
NSLog(@"addDone: %@\n", (addDone ? @"YES" : @"NO"));
if (addDone == YES) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0];
}
}
}
- (void)addHideKeyboardButtonToKeyboard {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
if (!keyboardWindow) return;
// Locate UIKeyboard.
UIView *foundKeyboard = nil;
for (UIView *possibleKeyboard in [keyboardWindow subviews]) {
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
foundKeyboard = possibleKeyboard;
break;
}
}
if (foundKeyboard) {
[self addButtonToKeyboard];
}
}
-(void)addButtonToKeyboard {
// create custom button
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(removeNumberKeyboard:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIView *keyboard;
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];
}
}
}
//Remove Number keyboard when you press DONE button
-(void)removeNumberKeyboard:(id)sender {
[self.view endEditing:TRUE];
}
//Remove Default keyboard when you press DONE button
-(IBAction)closeDefaultKeyBoard {
}
#pragma mark -
#pragma mark UITextFieldDelegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//if editing textfields, then do not add DONE button
if (mytextField.editing) {
addDone = NO;
}else{
addDone = YES;
}
NSLog(@"addDone: %@\n", (addDone ? @"YES" : @"NO"));
}
#pragma mark -
#pragma mark Other Lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc
{
[super dealloc];
[mynumberField release];
[mytextField release];
[doneButton release];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
__________________ iPhone Apps: Who SAid™, iFRICA™, iFA™, iReceipt™,eCash™