詳解iOS-按鈕單選與多選邏輯處理
我們經(jīng)常會有多行多列按鈕的頁面, 這個時(shí)候我們通常會選擇循環(huán)創(chuàng)建按鈕, 然后進(jìn)行按鈕單選或者多選的操作!
一. 單選邏輯處理

1. 創(chuàng)建按鈕控件數(shù)組及標(biāo)簽數(shù)組, 并升級當(dāng)前選中按鈕為屬性,方便使用
#define ZLUnselectedColor [UIColor colorWithRed:(241)/255.0 green:(242)/255.0 blue:(243)/255.0 alpha:1.0] #define ZLSelectedColor [UIColor colorWithRed:(108)/255.0 green:(187)/255.0 blue:(82)/255.0 alpha:1.0] @interface ZLRadioViewController () // 標(biāo)簽數(shù)組(按鈕文字) @property (nonatomic, strong) NSArray *markArray; // 按鈕數(shù)組 @property (nonatomic, strong) NSMutableArray *btnArray; // 選中按鈕 @property (nonatomic, strong) UIButton *selectedBtn; @end
#pragma mark - 懶加載
- (NSArray *)markArray {
if (!_markArray) {
NSArray *array = [NSArray array];
array = @[@"14", @"15", @"16", @"17", @"18"];
_markArray = array;
}
return _markArray;
}
- (NSMutableArray *)btnArray {
if (!_btnArray) {
NSMutableArray *array = [NSMutableArray array];
_btnArray = array;
}
return _btnArray;
}
2. 創(chuàng)建單選視圖, 循環(huán)創(chuàng)建按鈕, 并回顯上次選中值
- (void)setupRadioBtnView {
CGFloat UI_View_Width = [UIScreen mainScreen].bounds.size.width;
CGFloat marginX = 15;
CGFloat top = 100;
CGFloat btnH = 30;
CGFloat width = (250 - marginX * 4) / 3;
// 按鈕背景
UIView *btnsBgView = [[UIView alloc] initWithFrame:CGRectMake((UI_View_Width - 250) * 0.5, 50, 250, 300)];
btnsBgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:btnsBgView];
// 循環(huán)創(chuàng)建按鈕
NSInteger maxCol = 3;
for (NSInteger i = 0; i < 5; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = ZLUnselectedColor;
btn.layer.cornerRadius = 3.0; // 按鈕的邊框弧度
btn.clipsToBounds = YES;
btn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[btn setTitleColor:[UIColor colorWithRed:(102)/255.0 green:(102)/255.0 blue:(102)/255.0 alpha:1.0] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
NSInteger col = i % maxCol; //列
btn.x = marginX + col * (width + marginX);
NSInteger row = i / maxCol; //行
btn.y = top + row * (btnH + marginX);
btn.width = width;
btn.height = btnH;
[btn setTitle:self.markArray[i] forState:UIControlStateNormal];
[btnsBgView addSubview:btn];
btn.tag = i;
[self.btnArray addObject:btn];
}
// 創(chuàng)建完btn后再判斷是否能選擇(之前是已經(jīng)選取過的)
// 假數(shù)據(jù):之前已經(jīng)上傳16時(shí),則回顯16
for (UIButton *btn in btnsBgView.subviews) {
if ([@"16" isEqualToString:btn.titleLabel.text]) {
btn.selected = YES;
btn.backgroundColor = ZLSelectedColor;
break;
}
}
}
3. 數(shù)字按鈕單選處理, 根據(jù)tag值去判斷是否是當(dāng)前選中按鈕
- (void)chooseMark:(UIButton *)sender {
NSLog(@"點(diǎn)擊了%@", sender.titleLabel.text);
self.selectedBtn = sender;
sender.selected = !sender.selected;
for (NSInteger j = 0; j < [self.btnArray count]; j++) {
UIButton *btn = self.btnArray[j] ;
if (sender.tag == j) {
btn.selected = sender.selected;
} else {
btn.selected = NO;
}
btn.backgroundColor = ZLUnselectedColor;
}
UIButton *btn = self.btnArray[sender.tag];
if (btn.selected) {
btn.backgroundColor = ZLSelectedColor;
} else {
btn.backgroundColor = ZLUnselectedColor;
}
}
二. 多選邏輯處理

1. 創(chuàng)建按鈕控件數(shù)組和標(biāo)簽字典, 及選中標(biāo)簽數(shù)組(數(shù)字)和選中標(biāo)簽數(shù)組(文字字符串), 為了展示及上傳按鈕數(shù)據(jù)使用
#define ZLUnselectedColor [UIColor colorWithRed:(241)/255.0 green:(242)/255.0 blue:(243)/255.0 alpha:1.0] #define ZLSelectedColor [UIColor colorWithRed:(128)/255.0 green:(177)/255.0 blue:(34)/255.0 alpha:1.0] @interface ZLMultiselectController () // 標(biāo)簽數(shù)組 @property (nonatomic, strong) NSArray *markArray; // 標(biāo)簽字典 @property (nonatomic, strong) NSDictionary *markDict; // 選中標(biāo)簽數(shù)組(數(shù)字) @property (nonatomic, strong) NSMutableArray *selectedMarkArray; // 選中標(biāo)簽數(shù)組(文字字符串) @property (nonatomic, strong) NSMutableArray *selectedMarkStrArray; @end
#pragma mark - 懶加載
- (NSArray *)markArray {
if (!_markArray) {
NSArray *array = [NSArray array];
array = @[@"導(dǎo)購", @"客服", @"家教", @"禮儀", @"主持"];
_markArray = array;
}
return _markArray;
}
// 上傳通過文字key取數(shù)字value發(fā)送數(shù)字
- (NSDictionary *)markDict {
if (!_markDict) {
NSDictionary *dict = [NSDictionary dictionary];
dict = @{
@"導(dǎo)購" : @"3" ,
@"客服" : @"7",
@"家教" : @"9",
@"禮儀" : @"10",
@"主持" : @"11",
};
_markDict = dict;
}
return _markDict;
}
- (NSMutableArray *)selectedMarkArray {
if (!_selectedMarkArray) {
_selectedMarkArray = [NSMutableArray array];
}
return _selectedMarkArray;
}
- (NSMutableArray *)selectedMarkStrArray {
if (!_selectedMarkStrArray) {
_selectedMarkStrArray = [NSMutableArray array];
}
return _selectedMarkStrArray;
}
2.循環(huán)創(chuàng)建按鈕視圖, 循環(huán)創(chuàng)建按鈕
- (void)setupMultiselectView {
CGFloat UI_View_Width = [UIScreen mainScreen].bounds.size.width;
CGFloat marginX = 15;
CGFloat top = 19;
CGFloat btnH = 35;
CGFloat marginH = 40;
CGFloat height = 130;
CGFloat width = (UI_View_Width - marginX * 4) / 3;
// 按鈕背景
UIView *btnsBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, UI_View_Width, height)];
btnsBgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:btnsBgView];
// 循環(huán)創(chuàng)建按鈕
NSInteger maxCol = 3;
for (NSInteger i = 0; i < 5; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = ZLUnselectedColor;
btn.layer.cornerRadius = 3.0; // 按鈕的邊框弧度
btn.clipsToBounds = YES;
btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[btn setTitleColor:[UIColor colorWithRed:(102)/255.0 green:(102)/255.0 blue:(102)/255.0 alpha:1.0] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
NSInteger col = i % maxCol; //列
btn.x = marginX + col * (width + marginX);
NSInteger row = i / maxCol; //行
btn.y = top + row * (btnH + marginX);
btn.width = width;
btn.height = btnH;
[btn setTitle:self.markArray[i] forState:UIControlStateNormal];
[btnsBgView addSubview:btn];
}
// 確定按鈕
UIButton *surebtn = [UIButton buttonWithType:UIButtonTypeCustom];
[surebtn setTitle:@"確定" forState:UIControlStateNormal];
surebtn.frame = CGRectMake(marginX * 2, CGRectGetMaxY(btnsBgView.frame) + marginH, UI_View_Width - marginX * 4, 40);
surebtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[surebtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
surebtn.backgroundColor = [UIColor orangeColor];
surebtn.layer.cornerRadius = 3.0;
surebtn.clipsToBounds = YES;
[self.view addSubview:surebtn];
}
3. 按鈕多選邏輯處理, 并上傳數(shù)據(jù)請求處理
/**
* 按鈕多選處理
*/
- (void)chooseMark:(UIButton *)btn {
btn.selected = !btn.selected;
if (btn.isSelected) {
btn.backgroundColor = ZLSelectedColor;
[self.selectedMarkArray addObject:self.markDict[btn.titleLabel.text]];
[self.selectedMarkStrArray addObject:btn.titleLabel.text];
} else {
btn.backgroundColor = ZLUnselectedColor;
[self.selectedMarkArray removeObject:self.markDict[btn.titleLabel.text]];
[self.selectedMarkStrArray removeObject:btn.titleLabel.text];
}
}
/**
* 確認(rèn)接口請求處理
*/
- (void)sureBtnClick {
// 用戶選擇標(biāo)簽后就把值上傳, 也要傳給服務(wù)器下次直接請求回來
// 按鈕數(shù)字標(biāo)識字符串
NSString *numStr = [self.selectedMarkArray componentsJoinedByString:@","];
// 按鈕文字字符串
NSString *str = [self.selectedMarkStrArray componentsJoinedByString:@","];
// 測試:拼接請求參數(shù)
NSLog(@"按鈕數(shù)字標(biāo)識字符串:%@", numStr);
NSLog(@"按鈕文字字符串:%@", str);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
- 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- iOS開發(fā)中UISwitch按鈕的使用方法簡介
- 詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法
- iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
- iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法
- iOS實(shí)現(xiàn)全局懸浮按鈕
相關(guān)文章
iOS應(yīng)用設(shè)計(jì)模式開發(fā)中職責(zé)鏈(責(zé)任鏈)模式的實(shí)現(xiàn)解析
這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)模式開發(fā)中職責(zé)鏈模式的相關(guān)實(shí)現(xiàn)解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS基于UITableView實(shí)現(xiàn)多層展開與收起
這篇文章主要為大家詳細(xì)介紹了iOS基于UITableView實(shí)現(xiàn)多層展開與收起的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Observing?KVO?Key-Value基本使用原理示例詳解
這篇文章主要為大家介紹了Observing?KVO?Key-Value基本使用原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
iOS輕松實(shí)現(xiàn)導(dǎo)航欄透明漸變
這篇文章主要為大家詳細(xì)介紹了iOS輕松實(shí)現(xiàn)導(dǎo)航欄透明漸變效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

