转载

iOS开发,多个button数组,每个数组只能选中5项,多个数组只能选择3个。

由于常用xib,所以不想用代码写那么多个button。而且也懒的算位置

直接xib拉线成四个数组。水果,零食,饮料,甜点。

入题实现的功能就是,在这四个数组之中只能在3个数组只选中5项。有点绕(就比如你选择了水果和零食饮料数组中的button之后,就不能再选中甜点了);

读者自行理解吧。我不能表达的所有人都明白

iOS开发,多个button数组,每个数组只能选中5项,多个数组只能选择3个。

头部代码

 1 #import "CityViewController.h"  2   3 @interface CityViewController ()  4 //button数组  5 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *fruitsbtns;  6 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *snacksbtns;  7 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *drinksbtns;  8 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *dessertbtns;  9  10 //选中数组 11 @property (nonatomic ,strong) NSMutableArray * fruitsselects; 12 @property (nonatomic ,strong) NSMutableArray * snacksselects; 13 @property (nonatomic ,strong) NSMutableArray * drinksselects; 14 @property (nonatomic ,strong) NSMutableArray * dessertselects; 15 //button数组选中不能超过3个,选中的数组添加到totalarr数组中 16 @property (nonatomic ,strong) NSMutableArray *totalarr; 17  18 @end

初始化选中的数组。

 1 @implementation CityViewController  2   3 - (void)viewDidLoad {  4     [super viewDidLoad];  5       6     //初始化选中数组  7     self.fruitsselects      = [[NSMutableArray alloc] init];  8     self.snacksselects      = [[NSMutableArray alloc] init];  9     self.drinksselects      = [[NSMutableArray alloc] init]; 10     self.dessertselects     = [[NSMutableArray alloc] init]; 11      12      13      14 }

接下来就是点击事件了,

同样的点击事件也必须是拉线生成的。

同一个类里面的button所有点击事件都是在同一个方法里面。(水果类下的所有button在 fruitsclicked:里面)

(零食类下的所有button在 snacksclicked:里面)

(饮料类下的所有button在 drinksclicked:里面)

(甜点类下的所有button在 dessertclicked:里面)

我封装了一个方法,用于每个类选中的button加进选中的btn数组里面;

 1 - (void)select:(UIButton *)sender withselectmutablearray:(NSMutableArray *)mutablearray andarray:(NSArray *)array  2 {  3     //数组 还没选到2项的时候  4     if(self.totalarr.count <= 3)  5     {  6         for (UIButton *btn in array) {  7               8             if ([btn.currentTitle isEqualToString:sender.currentTitle]) {  9                 if (!btn.selected) { 10                     if (mutablearray.count <=4) { 11                         btn.selected = YES; 12                         [mutablearray addObject:btn]; 13                     }else 14                     { 15                         UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; 16                         [aler show]; 17                     } 18                      19                 }else 20                 { 21                     btn.selected = NO; 22                     [mutablearray removeObject:btn]; 23                      24                 } 25             } 26              27         } 28          29         //数组不等于0的时候。 30     }else if(self.totalarr.count != 0 && mutablearray.count != 0) 31     { 32         for (UIButton *btn in array) { 33              34             if ([btn.currentTitle isEqualToString:sender.currentTitle]) { 35                 if (!btn.selected) { 36                     if (mutablearray.count <=4) { 37                         btn.selected = YES; 38                         [mutablearray addObject:btn]; 39                     }else 40                     { 41                         UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; 42                         [aler show]; 43                     } 44                      45                 }else 46                 { 47                     btn.selected = NO; 48                     [mutablearray removeObject:btn]; 49                      50                 } 51             } 52              53         } 54          55          56     }else 57     { 58         UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; 59         [aler show]; 60     } 61      62      63 }

四个button数组类的点击事件:

先判断total是否为空。total是记录多个数组选中了多少

  1 //水果类点击事件   2 - (IBAction)fruitsClicked:(UIButton *)sender {   3        4        5     NSString *str = @"水果";   6        7     if(self.totalarr.count == 0)   8     {   9         [self.totalarr addObject:str];  10     }else  11     {  12         if ([self.totalarr containsObject:str]) {  13         }else  14         {  15             [self.totalarr addObject:str];  16         }  17     }  18       19     [self select:sender withselectmutablearray:self.fruitsselects andarray:self.fruitsbtns];  20       21     if(self.fruitsselects.count == 0)  22     {  23         [self.totalarr removeObject:str];  24     }  25       26   27       28 }  29   30   31 //零食类点击事件  32 - (IBAction)snacksClicked:(UIButton *)sender {  33       34     NSString *str = @"零食";  35       36     if(self.totalarr.count == 0)  37     {  38         [self.totalarr addObject:str];  39     }else  40     {  41         if ([self.totalarr containsObject:str]) {  42         }else  43         {  44             [self.totalarr addObject:str];  45         }  46     }  47       48     [self select:sender withselectmutablearray:self.snacksselects andarray:self.snacksbtns];  49       50     if(self.snacksselects.count == 0)  51     {  52         [self.totalarr removeObject:str];  53     }  54 }  55   56 //饮料类点击事件  57 - (IBAction)drinksClicked:(UIButton *)sender {  58       59     NSString *str = @"饮料";  60       61     if(self.totalarr.count == 0)  62     {  63         [self.totalarr addObject:str];  64     }else  65     {  66         if ([self.totalarr containsObject:str]) {  67         }else  68         {  69             [self.totalarr addObject:str];  70         }  71     }  72       73     [self select:sender withselectmutablearray:self.drinksselects andarray:self.drinksbtns];  74       75     if(self.drinksselects.count == 0)  76     {  77         [self.totalarr removeObject:str];  78     }  79       80 }  81   82 //甜点类点击事件  83 - (IBAction)dessertClicked:(UIButton *)sender {  84       85     NSString *str = @"甜点";  86       87     if(self.totalarr.count == 0)  88     {  89         [self.totalarr addObject:str];  90     }else  91     {  92         if ([self.totalarr containsObject:str]) {  93         }else  94         {  95             [self.totalarr addObject:str];  96         }  97     }  98       99     [self select:sender withselectmutablearray:self.dessertselects andarray:self.dessertbtns]; 100      101     if(self.dessertselects.count == 0) 102     { 103         [self.totalarr removeObject:str]; 104     } 105      106 }

很高兴。这是自己写的第一篇小功能小技术的实现。

以后会多多写点自己实现的小技术效能。望大家多多支持

谢谢;

正文到此结束
Loading...