iOS開發(fā)之多個按鈕選中的問題

問題介紹:

想要實現(xiàn)的功能有:

  1. 有多個按鈕
  2. 按鈕被點擊后為選中狀態(tài)
  3. 有按鈕被選中時點擊其他按鈕,之前被選中的按鈕狀態(tài)變?yōu)槲催x中,選中狀態(tài)轉移到新點擊的按鈕上
  4. 再次點擊被選中的按鈕后轉換為未選中狀態(tài)

解決辦法:

1. 設定多個按鈕

重點:能夠進行狀態(tài)轉換的重點在于設定一個tmpBtn,中間變量按鈕,作為保存按鈕狀態(tài)的中間變量。

/** 無散發(fā)按鈕*/
@property (nonatomic,strong)UIButton *button1;
/** 復現(xiàn)(微量)按鈕*/
@property (nonatomic,strong)UIButton *button2;
/** 復現(xiàn)(中等)按鈕*/
@property (nonatomic,strong)UIButton *button3;
/** 復現(xiàn)(濃烈)按鈕*/
@property (nonatomic,strong)UIButton *button4;
/** 采集模式按鈕*/
@property (nonatomic,strong)UIButton *button5;
/** 采集與復現(xiàn)(微量)按鈕*/
@property (nonatomic,strong)UIButton *button6;
/** 采集與復現(xiàn)(中等)按鈕*/
@property (nonatomic,strong)UIButton *button7;
/** 采集與復現(xiàn)(濃烈)按鈕*/
@property (nonatomic,strong)UIButton *button8;
/** 中間按鈕變量*/
@property (nonatomic,strong)UIButton *tmpBtn;

2. 懶加載

  1. 可以看到每個按鈕都添加了一個點擊的方法叫做click:,這就是改變狀態(tài)的重點。

  2. 我還對每個按鈕都設定了一個tag,用于標記每個按鈕。

# 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:@"復現(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:@"復現(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:@"復現(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:@"采集與復現(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:@"采集與復現(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:@"采集與復現(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)改變的邏輯(重點)

  1. 通過遍歷的方法對所有的按鈕進行遍歷,找出被點擊的那個,設置為selected = YES狀態(tài),對于未點擊的按鈕,設置為selected = NO狀態(tài)。

  2. 用中間變量tmpBtn對上次被點擊的按鈕進行存儲,在下一次被點擊時進行判斷,如果tmpBtn等于下一次被點擊的按鈕,說明用戶想取消上次按鈕的選擇,把tmpBtn清空。

  3. 如果上次點擊的按鈕和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)轉移
        {
            _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
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容