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;
}