链接地址:http://blog.sina.com.cn/s/blog_793f11ce0100teyj.html
在viewDidLoad 事件中绘制
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//绘制按钮
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; //绘制形状
//确定宽、高、X、Y坐标
CGRect frame;
frame.size.width=100;
frame.size.height=30;
frame.origin.x=320/2-50;
frame.origin.y=480/2-30;
[btn setFrame:frame];
//设置Tag(整形)
btn.tag=0;
//设置标题
[btn setTitle:@"按钮" forState:UIControlStateNormal];
//设置未按下和按下的图片切换
[btn setBackgroundImage:[UIImage imageNamed:@"bus.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"plane.png"] forState:UIControlStateHighlighted];
//设置事件
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
//设置背景色和透明度
//[btn setBackgroundColor:[UIColor blackColor]];
//[btn setAlpha:0.8];
[self.view addSubview:btn];
//[btn release];
}
//按钮响应事件
-(void) btnPressed:(id) sender{
UIButton *myBtn=(UIButton *) sender;
if (myBtn.tag==0) {
//预留模块
}
}