Cocoa-Matic

iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Thursday, August 13, 2009

Add decimal point to number pad

Add this to viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowWeight:) name:UIKeyboardDidShowNotification object:nil];

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideWeight:) name:UIKeyboardDidHideNotification object:nil];


Add these methods:

- (void)addDecimalPoint:(id)sender {

NSLog(@"decimal point pressed");

BOOL allowDecimal = YES;

unichar c;

unichar d = '.';

for(int i=0;i<[todayWeighInTextField.text length];i++) {

c = [todayWeighInTextField.text characterAtIndex:i];

if(c == d) allowDecimal = NO;

}

if(allowDecimal) todayWeighInTextField.text = [todayWeighInTextField.text stringByAppendingString:@"."];

}


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

[self.keyboardToolbar setHidden:NO];

}


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

UIWindow *tmpWindow;

UIViewController *mainViewController = self;

UIView *keyboard;

for(int i=0;i<[[[UIApplication sharedApplication] windows] count];i++) {

tmpWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:i];

for(int j=0;j<[tmpWindow.subviews count];j++) {

keyboard = [tmpWindow.subviews objectAtIndex:j];

if([[keyboard description] hasPrefix:@"] == YES) {

UIButton *decimalPointBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

decimalPointBtn.frame = CGRectMake(0, 164, 106, 53);

[decimalPointBtn setImage:[UIImage imageNamed:@"decimalPointNormal.png"] forState:UIControlStateNormal];

[decimalPointBtn setImage:[UIImage imageNamed:@"decimalPointPressed.png"] forState:UIControlStateHighlighted];

[keyboard addSubview:decimalPointBtn];

[decimalPointBtn addTarget:mainViewController action:@selector(addDecimalPoint:) forControlEvents:UIControlEventTouchUpInside];

return;

}

}

}

}


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

[[NSNotificationCenter defaultCenter] removeObserver:self];

}


Labels: ,


 
Newer Entries »