实例代码(共两段,分别展示了属性与常用方法,这两段都只是ViewController.m)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) createUIRectButton
{
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 40);
[btn setTitle:@"按钮01" forState:UIControlStateNormal];
[btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
btn.backgroundColor = [UIColor grayColor];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
[btn setTintColor:[UIColor whiteColor]];
btn.titleLabel.font = [UIFont systemFontOfSize:12];
[self.view addSubview:btn];
}
- (void) createImageBtn
{
UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(100, 200, 100, 100);
UIImage* icon01 = [UIImage imageNamed:@"aa.jpg"];
UIImage* icon02 = [UIImage imageNamed:@"timg.jpeg"];
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createUIRectButton];
[self createImageBtn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) createBtn
{
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = 100;
btn2.tag = 101;
btn3.tag = 102;
btn.frame = CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
btn2.frame = CGRectMake(100, 140, 80, 40);
[btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
btn3.frame = CGRectMake(100, 180, 80, 40);
[btn3 setTitle:@"按钮3" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchUpOutside];
[btn3 addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
}
- (void) pressBtn1:(UIButton*) btn
{
if (btn.tag == 100)
{
NSLog(@"按钮被按下1");
}
if (btn.tag == 101)
{
NSLog(@"按钮被按下2");
}
if (btn.tag == 102)
{
NSLog(@"按钮被按下3");
}
}
- (void) pressBtn2
{
NSLog(@"按钮被按下2");
}
- (void) pressBtn3
{
NSLog(@"按钮被按下3");
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createBtn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- 首先,虽然我们叫圆角按钮,但现在圆角没有圆角了,就是普通的方形按钮
- 对于按钮,基本有文字和状态两个参数
- 在插图片的时候,我不知道为什么只能jpg和jepg对于png图片插不进去,百度的时候,并不能看懂,留给有缘人。
- 对于按钮触发事件时,我们又分3种状态
- UIControlEventTouchUpInside当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数
- UIControlEventTouchUpOutside当手指离开屏幕时并且手指的位置在按钮范围外触发事件函数
- UIControlEventTouchDown当手指触碰到屏幕时并且手指的位置在按钮范围内触发事件函数
- 在写触发事件函数时,我使用了- (void) pressBtn1:(UIButton*) btn函数,这样自定义函数的好处在于它将自身作为返回值又返回了,这样我们给按钮的tag属性安排一个数字编号(相当于独一无二的身份证)就可以进行判断,对于不同的按钮在一个函数中安排不同的触发,就不用每个都写。