1、初始化按钮并设置x,y,宽,高
2、设置按钮背景图片
setBackgroundImage 添加UIImage
3、给按键添加点击事件:
addTarget:self 添加目标 action:@selector(exitButton) 触发方法 exitButton为方法名 forControlEvents:UIControlEventTouchUpInside 触发事件:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside 单击并抬起
UIControlEventTouchUpOutside
UIControlEventTouchCancel
4、设置按钮tag值
5、添加按钮到当前view控件中
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(300, 20, 15, 15)];
[button setBackgroundImage:[UIImage imageNamed:@"clearbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(exitButton) forControlEvents:UIControlEventTouchUpInside];
button.tag = 999;
[self.view addSubview:button];