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

問題介紹:

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

  1. 有多個按鈕
  2. 按鈕被點擊后為選中狀態(tài)
  3. 有按鈕被選中時點擊其他按鈕,之前被選中的按鈕狀態(tài)變?yōu)槲催x中虫碉,選中狀態(tài)轉(zhuǎn)移到新點擊的按鈕上
  4. 再次點擊被選中的按鈕后轉(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. 懶加載

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

  2. 我還對每個按鈕都設(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)改變的邏輯(重點)

  1. 通過遍歷的方法對所有的按鈕進行遍歷者娱,找出被點擊的那個,設(shè)置為selected = YES狀態(tài)苏揣,對于未點擊的按鈕黄鳍,設(shè)置為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)轉(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吞加,隨后出現(xiàn)的幾起案子裙犹,更是在濱河造成了極大的恐慌,老刑警劉巖衔憨,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叶圃,死亡現(xiàn)場離奇詭異,居然都是意外死亡践图,警方通過查閱死者的電腦和手機掺冠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來码党,“玉大人德崭,你說我怎么就攤上這事∫九蹋” “怎么了眉厨?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長兽狭。 經(jīng)常有香客問我憾股,道長,這世上最難降的妖魔是什么椭符? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任荔燎,我火速辦了婚禮,結(jié)果婚禮上销钝,老公的妹妹穿的比我還像新娘有咨。我一直安慰自己,他們只是感情好蒸健,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布座享。 她就那樣靜靜地躺著,像睡著了一般似忧。 火紅的嫁衣襯著肌膚如雪渣叛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天盯捌,我揣著相機與錄音淳衙,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛箫攀,可吹牛的內(nèi)容都是我干的肠牲。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼靴跛,長吁一口氣:“原來是場噩夢啊……” “哼缀雳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起梢睛,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤肥印,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后绝葡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體深碱,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年挤牛,在試婚紗的時候發(fā)現(xiàn)自己被綠了莹痢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片种蘸。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡墓赴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出航瞭,到底是詐尸還是另有隱情诫硕,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布刊侯,位于F島的核電站章办,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏滨彻。R本人自食惡果不足惜藕届,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亭饵。 院中可真熱鬧休偶,春花似錦、人聲如沸辜羊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽八秃。三九已至碱妆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昔驱,已是汗流浹背疹尾。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人纳本。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓睡雇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親饮醇。 傳聞我的和親對象是個殘疾皇子它抱,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容