UIButton
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor= [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
[_windowrelease];
//用button自己的便利构造器的方式来创建对象
UIButton*button = [UIButtonbuttonWithType:UIButtonTypeSystem];
//指定button的位置和大小
button.frame=CGRectMake(20,100,100,30);
button.backgroundColor= [UIColorgreenColor];
[self.windowaddSubview:button];
// button不用release
// [button release];
//给button设置标题
[buttonsetTitle:@"确认"forState:UIControlStateNormal];
[buttonsetTitle:@"王俊太脏了"forState:UIControlStateHighlighted];
//标题的字体大小
button.titleLabel.font= [UIFontsystemFontOfSize:14];
//圆角
button.layer.cornerRadius=10;
button.layer.borderWidth=1;
//点击方法Button最重要的部分
[buttonaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
returnYES;
}
- (void)click:(UIButton*)button{
NSLog(@"王俊太脏了");
}
//更换按钮的背景图
- (void)changePic:(UIButton*)button{
//判断
if(self.isSelected) {
[buttonsetBackgroundImage:[UIImageimageNamed:@"check.png"]forState:UIControlStateNormal];
}else{
[buttonsetBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];
}
//把当前的状态也进行调整
self.isSelected= !self.isSelected;
-(void)click:(UIButton*)button{
//按钮的点击方法会全过来一个相应类型的button,谁触发了这个点击方法,相应的button就是那个对象
UIButton*but =(UIButton*) [self.windowviewWithTag:1000];
//判断当前按钮的标题
// currentTitle获取当前按钮的标题
NSLog(@"%@",but.currentTitle);
if([but.currentTitleisEqualToString:@"确认"]) {
[butsetTitle:@"取消"forState:UIControlStateNormal];
}else{
[butsetTitle:@"确认"forState:UIControlStateNormal];
}
NSLog(@"%ld",button.tag);
if([button.currentTitleisEqualToString:@"确认"]) {
[buttonsetTitle:@"取消"forState:UIControlStateNormal];
}else{
[buttonsetTitle:@"确认"forState:UIControlStateNormal];
}
};
[button1setImage:[UIImageimageNamed:@"BtnOff"]forState:UIControlStateNormal];
//如果想使用setImage设置图片的话,button的类型要调整成custom,setImage方法不会把图片放大成按钮大小
//给button设置背景图片
[buttonsetBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];
self.isSelected=YES;