在根视图控制器类的.h文件中
#import <UIKit/UIKit.h>
@interface liViewController : UIViewController
//添加按钮
- (void) addButton;
@end
在.m文件中
@implementation liViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//调用自己的方法
[self addButton];
}
- (void) addButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 200, 120, 40);
[button setTitle:@"点击按钮" forState:UIControlStateNormal];
//给按钮添加关联动作
[button addTarget:self action:@selector(addAlertButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// [button release];
}
- (void)addAlertButton:(id)sender
{ //添加动态按钮
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"点击了动态按钮" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//动态按钮的显示
[alertView show];
[alertView release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end