- (void)viewDidLoad {
[super viewDidLoad];
//创建一个按钮
UIButton * button = [[UIButton alloc] init];
//设置按钮的位置和大小
button.frame = CGRectMake(100, 100, 250, 250);
//设置默认按钮文字
[button setTitle:@"点我试试" forState:UIControlStateNormal];
//设置默认按钮文字颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//设置默认背景
[button setBackgroundImage:[UIImage imageNamed:@"luffy"] forState:UIControlStateNormal];
//设置高亮状态下的文字
[button setTitle:@"再点我砍了你" forState:UIControlStateHighlighted];
//设置高亮状态下的文字的颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//设置高亮状态下的背景
[button setBackgroundImage:[UIImage imageNamed:@"zoro"] forState:UIControlStateHighlighted];
//把创建的button对象添加到self.view
[self.view addSubview:button];
// 4.监听按钮点击(点击按钮后就会调⽤用self的buttonClicked⽅方法)
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonClicked:(UIButton *)button{
NSLog(@"按钮被点击了,文字是:%@",button.currentTitle);
}
|
|