//手工添加按钮以及为按钮添加addTarget目标动作关联开始
CGRect frame = CGRectMake(10.0f, 10.0f, 120.0f, 50.0f);
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"title"forState:UIControlStateNormal];
button.backgroundColor = [UIColorclearColor];
button.tag = 2000; //区分同种控件的标记
[button addTarget:selfaction:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//selector后面的方法后一定不要忘了要加个冒号 ,因为定义的buttonClicked方式后面是带了类型为id的参数
[self.view addSubview:button];
//手工添加按钮结束
-(IBAction)buttonClicked:(id)sender
{
NSLog(@"button Clicked!");
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"title"message:@"message"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:@"otherbuttontitles", nil];
[alert show];
[alert release];
}