Thursday, November 11, 2010

Create a UIButton and respond to the touch


Here's the basic code to create a text button and do something when it's touched:

// add the button to the view
-(void)buildButton {
 UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] initWithFrame:CGRectMake(0, 0, 150, 25)];
 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
 [button setTitle:@"CLICK IT" forState:UIControlStateNormal];
 [button setCenter:CGPointMake( 320 / 2, 480 - 25 )];
 [self addSubview:button];
}

// respond to the button click
-(void)buttonClick:(UIView*)clickedButton
{
 NSLog(@"CLICK!");
}

No comments:

Post a Comment

Followers