UIButton
import “AppDelegate.h”
@interface AppDelegate ()
@property(nonatomic,assign)BOOL isSelected;
@property(nonatomic,assign)BOOL isClick;
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorcyanColor];
[self.windowmakeKeyAndVisible];
[_window release];// 用button自己的便利构造器的方式来创建对象
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeSystem];
// 指定button的位置和大小
button.frame=CGRectMake(138,100, 100, 80);
// 设置背景颜色
button.backgroundColor=[UIColorredColor];
[self.windowaddSubview:button];
// button不用release// 给button设置标题
[button setTitle:@”张三SB吗?”forState:UIControlStateNormal];
// 给字体添加颜色
[button setTitleColor:[UIColoryellowColor] forState:UIControlStateNormal];// 标题的字体大小
button.titleLabel.font=[UIFontsystemFontOfSize:20];// 设置圆角
button.layer.cornerRadius=20;// 边框
button.layer.borderWidth=1;// 点击方法 butten最重要的方法
[button addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
// 重新创建一个button
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(50,50, 150, 90);
button.backgroundColor=[UIColorredColor];
[self.windowaddSubview:button];
[button setTitle:@"确认"forState:UIControlStateNormal];
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
button.layer.borderWidth=1;
button.layer.cornerRadius=10;
button.titleLabel.font=[UIFontsystemFontOfSize:20];
[button addTarget:selfaction:@selector(changePic:)forControlEvents:UIControlEventTouchUpInside];
// 设置一个tag值
button.tag=1000;
// 给button设置背景图片
[button setBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];
self.isSelected=YES;
UIButton *button1=[UIButtonbuttonWithType:UIButtonTypeCustom];
button1.frame=CGRectMake(100,300, 50, 40);
button1.backgroundColor=[UIColoryellowColor];
[self.windowaddSubview:button1];
[button1 setTitle:@"测试"forState:UIControlStateNormal];
[button1 setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
button1.layer.borderWidth=1;
button1.layer.cornerRadius=10;
button1.titleLabel.font=[UIFontsystemFontOfSize:20];
[button1 addTarget:selfaction:@selector(changeImage:)forControlEvents:UIControlEventTouchUpInside];
button1.tag=1001;
// 给button设置背景图片
[button setBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];
[button1 setImage:[UIImageimageNamed:@"BtnOff.png"]forState:UIControlStateNormal];
// 如果想使用setImage设置图片的话,button的类型要调整成,custom,setImage方法不会把图片放大成按钮大小
self.isClick=NO;
return YES;
}
-(void)click:(UIButton *)button{
NSLog(@”恩,他是SB!!!”);
}
// 更换按钮的背景图片
-(void)changePic:(UIButton *)button{
// 判断
// 根据不同的状态更换图片
if (self.isSelected) {
[button setBackgroundImage:[UIImageimageNamed:@”check.png”]forState: UIControlStateNormal];
}else{
[button setBackgroundImage:[UIImageimageNamed:@”checked.png”]forState: UIControlStateNormal];
}
// 把当前的状态也进行调整
// 取反
self.isSelected=!self.isSelected;
}
-(void)changeImage:(UIButton *)button{
if (self.isClick) {
[button setImage:[UIImageimageNamed:@”BtnOff.png”]forState:UIControlStateNormal];
}else{
[button setImage:[UIImageimageNamed:@”BtnOn.png”]forState:UIControlStateNormal];
}
self.isClick=!self.isClick;
}