問題介紹:
想要實現(xiàn)的功能有:
- 有多個按鈕
- 按鈕被點擊后為選中狀態(tài)
- 有按鈕被選中時點擊其他按鈕,之前被選中的按鈕狀態(tài)變?yōu)槲催x中虫碉,選中狀態(tài)轉(zhuǎn)移到新點擊的按鈕上
- 再次點擊被選中的按鈕后轉(zhuǎn)換為未選中狀態(tài)
解決辦法:
1. 設(shè)定多個按鈕
重點:能夠進行狀態(tài)轉(zhuǎn)換的重點在于設(shè)定一個tmpBtn
呛哟,中間變量按鈕叠荠,作為保存按鈕狀態(tài)的中間變量。
/** 無散發(fā)按鈕*/
@property (nonatomic,strong)UIButton *button1;
/** 復(fù)現(xiàn)(微量)按鈕*/
@property (nonatomic,strong)UIButton *button2;
/** 復(fù)現(xiàn)(中等)按鈕*/
@property (nonatomic,strong)UIButton *button3;
/** 復(fù)現(xiàn)(濃烈)按鈕*/
@property (nonatomic,strong)UIButton *button4;
/** 采集模式按鈕*/
@property (nonatomic,strong)UIButton *button5;
/** 采集與復(fù)現(xiàn)(微量)按鈕*/
@property (nonatomic,strong)UIButton *button6;
/** 采集與復(fù)現(xiàn)(中等)按鈕*/
@property (nonatomic,strong)UIButton *button7;
/** 采集與復(fù)現(xiàn)(濃烈)按鈕*/
@property (nonatomic,strong)UIButton *button8;
/** 中間按鈕變量*/
@property (nonatomic,strong)UIButton *tmpBtn;
2. 懶加載
可以看到每個按鈕都添加了一個點擊的方法叫做
click:
扫责,這就是改變狀態(tài)的重點榛鼎。我還對每個按鈕都設(shè)定了一個tag,用于標(biāo)記每個按鈕鳖孤。
# pragma mark - 懶加載
- (UIButton *)button1{
if (!_button1) {
_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
_button1.tag = 100 + 1;
[_button1 setTitle:@"無散發(fā)" forState:UIControlStateNormal];
_button1.titleLabel.font = [UIFont systemFontOfSize:16];
_button1.layer.cornerRadius = 2;
_button1.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button1];
}
return _button1;
}
- (UIButton *)button2{
if (!_button2) {
_button2 = [UIButton buttonWithType:UIButtonTypeCustom];
_button2.tag = 100 + 2;
[_button2 setTitle:@"復(fù)現(xiàn)(微量)" forState:UIControlStateNormal];
_button2.titleLabel.font = [UIFont systemFontOfSize:16];
_button2.layer.cornerRadius = 2;
_button2.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button2];
}
return _button2;
}
- (UIButton *)button3{
if (!_button3) {
_button3 = [UIButton buttonWithType:UIButtonTypeCustom];
_button3.tag = 100 + 3;
[_button3 setTitle:@"復(fù)現(xiàn)(中等)" forState:UIControlStateNormal];
_button3.titleLabel.font = [UIFont systemFontOfSize:16];
_button3.layer.cornerRadius = 2;
_button3.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button3];
}
return _button3;
}
- (UIButton *)button4{
if (!_button4) {
_button4 = [UIButton buttonWithType:UIButtonTypeCustom];
_button4.tag = 100 + 4;
[_button4 setTitle:@"復(fù)現(xiàn)(濃烈)" forState:UIControlStateNormal];
_button4.titleLabel.font = [UIFont systemFontOfSize:16];
_button4.layer.cornerRadius = 2;
_button4.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button4 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button4];
}
return _button4;
}
- (UIButton *)button5{
if (!_button5) {
_button5 = [UIButton buttonWithType:UIButtonTypeCustom];
_button5.tag = 100 + 5;
[_button5 setTitle:@"采集模式" forState:UIControlStateNormal];
_button5.titleLabel.font = [UIFont systemFontOfSize:16];
_button5.layer.cornerRadius = 2;
_button5.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button5 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button5];
}
return _button5;
}
- (UIButton *)button6{
if (!_button6) {
_button6 = [UIButton buttonWithType:UIButtonTypeCustom];
_button6.tag = 100 + 6;
[_button6 setTitle:@"采集與復(fù)現(xiàn)(微量)" forState:UIControlStateNormal];
_button6.titleLabel.font = [UIFont systemFontOfSize:16];
_button6.layer.cornerRadius = 2;
_button6.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button6 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button6 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button6];
}
return _button6;
}
- (UIButton *)button7{
if (!_button7) {
_button7 = [UIButton buttonWithType:UIButtonTypeCustom];
_button7.tag = 100 + 7;
[_button7 setTitle:@"采集與復(fù)現(xiàn)(中等)" forState:UIControlStateNormal];
_button7.titleLabel.font = [UIFont systemFontOfSize:16];
_button7.layer.cornerRadius = 2;
_button7.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button7 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button7 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button7];
}
return _button7;
}
- (UIButton *)button8{
if (!_button8) {
_button8 = [UIButton buttonWithType:UIButtonTypeCustom];
_button8.tag = 100 + 8;
[_button8 setTitle:@"采集與復(fù)現(xiàn)(濃烈)" forState:UIControlStateNormal];
_button8.titleLabel.font = [UIFont systemFontOfSize:16];
_button8.layer.cornerRadius = 2;
_button8.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
[_button8 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_button8 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button8];
}
return _button8;
}
3. 按鈕狀態(tài)改變的邏輯(重點)
通過遍歷的方法對所有的按鈕進行遍歷者娱,找出被點擊的那個,設(shè)置為
selected = YES
狀態(tài)苏揣,對于未點擊的按鈕黄鳍,設(shè)置為selected = NO
狀態(tài)。用中間變量
tmpBtn
對上次被點擊的按鈕進行存儲平匈,在下一次被點擊時進行判斷框沟,如果tmpBtn
等于下一次被點擊的按鈕,說明用戶想取消上次按鈕的選擇增炭,把tmpBtn
清空街望。如果上次點擊的按鈕和
tmpBtn
不同,說明用戶想要更換選中的按鈕弟跑,于是將tmpBtn
等于下一次點擊的按鈕灾前。
# pragma mark - 點擊按鈕邏輯
- (void)click:(UIButton *)sender {
for (int i=0; i < 9; i++) {
UIButton *btn = (UIButton *)[self viewWithTag:100+i];
if (sender.tag - 100 == i && _tmpBtn == nil)
//按鈕第一次被點擊,被選中
{
btn.selected = YES;
_tmpBtn = sender;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[btn setBackgroundColor:[UIColor colorWithRed:0/255.0 green:132/255.0 blue:255/255.0 alpha:1]];
}else if (_tmpBtn != nil && sender.tag - 100 == i && _tmpBtn == sender)
//按鈕第二次被點擊孟辑,選中狀態(tài)取消
{
btn.selected = NO;
_tmpBtn = nil;
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1]];
}else if (_tmpBtn != nil && sender.tag - 100 == i && _tmpBtn != sender)
//有按鈕被選中時哎甲,點擊了其他按鈕蔫敲,將選中狀態(tài)轉(zhuǎn)移
{
_tmpBtn = sender;
btn.selected = YES;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[btn setBackgroundColor:[UIColor colorWithRed:0/255.0 green:132/255.0 blue:255/255.0 alpha:1]];
}else
//有按鈕被選中時,點擊了其他按鈕炭玫,無效
{
btn.selected = NO;
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1]];
}
}
}
圖示
永遠只能有沒有按鈕被選中
和只有一個按鈕被選中
這兩種狀態(tài)奈嘿。
image.png
image.png
image.png