Cocoa-Matic

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

Thursday, August 12, 2010

UIButton with custom UIImage

Most developers want to steer clear of the default buttons provided in Cocoa because they're just not that attractive. The following code shows how to programmatically create an image button and define an action for when the button is pressed.
// Create button and set frame
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
// Set button image, in this case "my_image.png"
[myButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"my_image" ofType:@"png"]] forState:UIControlStateNormal];
// Call buttonWasPressed function when button is touched
[myButton addTarget:self action:@selector(buttonWasPressed) forControlEvents:UIControlEventTouchUpInside];
// Add the button to view
[self.view addSubview:myButton];

Labels: , ,

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: ,