服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 详解iOS-按钮单选与多选逻辑处理

详解iOS-按钮单选与多选逻辑处理

2021-03-11 16:01smile丽语 IOS

本篇文章主要介绍了详解iOS-按钮单选与多选逻辑处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

我们经常会有多行多列按钮的页面, 这个时候我们通常会选择循环创建按钮, 然后进行按钮单选或者多选的操作!

一. 单选逻辑处理

详解iOS-按钮单选与多选逻辑处理

1. 创建按钮控件数组及标签数组, 并升级当前选中按钮为属性,方便使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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 ()
 
// 标签数组(按钮文字)
@property (nonatomic, strong) nsarray *markarray;
 
// 按钮数组
@property (nonatomic, strong) nsmutablearray *btnarray;
 
// 选中按钮
@property (nonatomic, strong) uibutton *selectedbtn;
 
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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. 创建单选视图, 循环创建按钮, 并回显上次选中值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
- (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];
 
  // 循环创建按钮
  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];
  }
 
  // 创建完btn后再判断是否能选择(之前是已经选取过的)
  // 假数据:之前已经上传16时,则回显16
  for (uibutton *btn in btnsbgview.subviews) {
    if ([@"16" isequaltostring:btn.titlelabel.text]) {
      btn.selected = yes;
      btn.backgroundcolor = zlselectedcolor;
      break;
    }
  }
}

3. 数字按钮单选处理, 根据tag值去判断是否是当前选中按钮

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (void)choosemark:(uibutton *)sender {
  nslog(@"点击了%@", 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;
  }
}

二. 多选逻辑处理

详解iOS-按钮单选与多选逻辑处理

1. 创建按钮控件数组和标签字典, 及选中标签数组(数字)和选中标签数组(文字字符串), 为了展示及上传按钮数据使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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 ()
 
// 标签数组
@property (nonatomic, strong) nsarray *markarray;
 
// 标签字典
@property (nonatomic, strong) nsdictionary *markdict;
 
// 选中标签数组(数字)
@property (nonatomic, strong) nsmutablearray *selectedmarkarray;
 
// 选中标签数组(文字字符串)
@property (nonatomic, strong) nsmutablearray *selectedmarkstrarray;
 
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma mark - 懒加载
 
- (nsarray *)markarray {
  if (!_markarray) {
    nsarray *array = [nsarray array];
    array = @[@"导购", @"客服", @"家教", @"礼仪", @"主持"];
    _markarray = array;
  }
  return _markarray;
}
 
// 上传通过文字key取数字value发送数字
- (nsdictionary *)markdict {
  if (!_markdict) {
    nsdictionary *dict = [nsdictionary dictionary];
    dict = @{
         @"导购" : @"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.循环创建按钮视图, 循环创建按钮

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
- (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];
 
  // 循环创建按钮
  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. 按钮多选逻辑处理, 并上传数据请求处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * 按钮多选处理
 */
- (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];
  }
}
 
/**
 * 确认接口请求处理
 */
- (void)surebtnclick {
  // 用户选择标签后就把值上传, 也要传给服务器下次直接请求回来
  // 按钮数字标识字符串
  nsstring *numstr = [self.selectedmarkarray componentsjoinedbystring:@","];
  // 按钮文字字符串
  nsstring *str = [self.selectedmarkstrarray componentsjoinedbystring:@","];
 
  // 测试:拼接请求参数
  nslog(@"按钮数字标识字符串:%@", numstr);
  nslog(@"按钮文字字符串:%@", str);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/ba9b53af81c8

延伸 · 阅读

精彩推荐
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22