UIButton 继承于 UIControl , UIControl 继承于 UIView.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor purpleColor];
[self.window makeKeyAndVisible];
[self.window release];
// 创建一个UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(150, 100, 80, 80)
;
button.backgroundColor = [UIColor whiteColor];
[self.window addSubview:button];
[button setTitle:@"确认" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:25];
button.layer.cornerRadius = 8;
button.layer.masksToBounds = YES;
[button addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1000;
self.isSelected = YES;
// 给Button设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
UIButton *buttontwo = [UIButton buttonWithType:UIButtonTypeCustom];
buttontwo.frame = CGRectMake(150, 220, 80, 80);
[self.window addSubview:button1];
// buttontwo.layer.cornerRadius = 8;
// [buttontwo setTitle:@"测试" forState:UIControlStateNormal];
// buttontwo.titleLabel.font = [UIFont systemFontOfSize:10];
[buttontwo addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
buttontwo.tag = 1001;
// 设置前景图片
[buttontwo setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
self.isClick = NO;
return YES;
}
- (void)click:(UIButton *)button{
//谁触发了按钮,相应的button就是那个对象.
// 1.不管按谁,只修改的是tag是1000按钮的内容
UIButton *but = (UIButton *)[self.window viewWithTag:1000];
// 判断当前按钮的标题
//currentTitle -- 获取当前按钮标题.
if ([but.currentTitle compare:@"确认"] == 0) {
[but setTitle:@"取消" forState:UIControlStateNormal];
}else{
[but setTitle:@"确认" forState:UIControlStateNormal];
}
// 打印出button当前的标题.
NSLog(@"%@",but.currentTitle);
//2.按哪个按钮,哪个按钮的内容就会改变
if ([button.currentTitle isEqualToString:@"确认"]) {
[button setTitle:@"取消" forState:UIControlStateNormal];
}else{
[button setTitle:@"确认" forState:UIControlStateNormal];
}
NSLog(@"%ld",button.tag);
定义一条 @property(nonatomic, assign)BOOL isSelected; 的属性
// 更换按钮的背景图方法.
- (void)changePic:(UIButton *)button{
//判断
if (self.isSelected) {
[button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}else{
[button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
// 最后别忘了把当前的状态进行调整.
self.isSelected = !self.isSelected;
if ([button.currentTitle isEqualToString:@"确认"]) {
[button setTitle:@"取消" forState:UIControlStateNormal];
}else{
[button setTitle:@"确认" forState:UIControlStateNormal];
}
}
}
定义一条@property(nonatomic, assign)BOOL isClick;的属性.
- (void)changeImage:(UIButton *)button{
//更换前景图片
if (self.isClick) {
[button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
}else{
[button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
}
self.isClick = !self.isClick;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-25 03:50:49