由于常用xib,所以不想用代码写那么多个button。而且也懒的算位置
直接xib拉线成四个数组。水果,零食,饮料,甜点。
入题实现的功能就是,在这四个数组之中只能在3个数组只选中5项。有点绕(就比如你选择了水果和零食饮料数组中的button之后,就不能再选中甜点了);
读者自行理解吧。我不能表达的所有人都明白
头部代码
#import "CityViewController.h" @interface CityViewController ()
//button数组
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *fruitsbtns;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *snacksbtns;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *drinksbtns;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *dessertbtns; //选中数组
@property (nonatomic ,strong) NSMutableArray * fruitsselects;
@property (nonatomic ,strong) NSMutableArray * snacksselects;
@property (nonatomic ,strong) NSMutableArray * drinksselects;
@property (nonatomic ,strong) NSMutableArray * dessertselects;
//button数组选中不能超过3个,选中的数组添加到totalarr数组中
@property (nonatomic ,strong) NSMutableArray *totalarr; @end
初始化选中的数组。
@implementation CityViewController - (void)viewDidLoad {
[super viewDidLoad]; //初始化选中数组
self.fruitsselects = [[NSMutableArray alloc] init];
self.snacksselects = [[NSMutableArray alloc] init];
self.drinksselects = [[NSMutableArray alloc] init];
self.dessertselects = [[NSMutableArray alloc] init]; }
接下来就是点击事件了,
同样的点击事件也必须是拉线生成的。
同一个类里面的button所有点击事件都是在同一个方法里面。(水果类下的所有button在 fruitsclicked:里面)
(零食类下的所有button在 snacksclicked:里面)
(饮料类下的所有button在 drinksclicked:里面)
(甜点类下的所有button在 dessertclicked:里面)
我封装了一个方法,用于每个类选中的button加进选中的btn数组里面;
- (void)select:(UIButton *)sender withselectmutablearray:(NSMutableArray *)mutablearray andarray:(NSArray *)array
{
//数组 还没选到2项的时候
if(self.totalarr.count <= )
{
for (UIButton *btn in array) { if ([btn.currentTitle isEqualToString:sender.currentTitle]) {
if (!btn.selected) {
if (mutablearray.count <=) {
btn.selected = YES;
[mutablearray addObject:btn];
}else
{
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aler show];
} }else
{
btn.selected = NO;
[mutablearray removeObject:btn]; }
} } //数组不等于0的时候。
}else if(self.totalarr.count != && mutablearray.count != )
{
for (UIButton *btn in array) { if ([btn.currentTitle isEqualToString:sender.currentTitle]) {
if (!btn.selected) {
if (mutablearray.count <=) {
btn.selected = YES;
[mutablearray addObject:btn];
}else
{
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aler show];
} }else
{
btn.selected = NO;
[mutablearray removeObject:btn]; }
} } }else
{
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aler show];
} }
四个button数组类的点击事件:
先判断total是否为空。total是记录多个数组选中了多少
//水果类点击事件
- (IBAction)fruitsClicked:(UIButton *)sender { NSString *str = @"水果"; if(self.totalarr.count == )
{
[self.totalarr addObject:str];
}else
{
if ([self.totalarr containsObject:str]) {
}else
{
[self.totalarr addObject:str];
}
} [self select:sender withselectmutablearray:self.fruitsselects andarray:self.fruitsbtns]; if(self.fruitsselects.count == )
{
[self.totalarr removeObject:str];
} } //零食类点击事件
- (IBAction)snacksClicked:(UIButton *)sender { NSString *str = @"零食"; if(self.totalarr.count == )
{
[self.totalarr addObject:str];
}else
{
if ([self.totalarr containsObject:str]) {
}else
{
[self.totalarr addObject:str];
}
} [self select:sender withselectmutablearray:self.snacksselects andarray:self.snacksbtns]; if(self.snacksselects.count == )
{
[self.totalarr removeObject:str];
}
} //饮料类点击事件
- (IBAction)drinksClicked:(UIButton *)sender { NSString *str = @"饮料"; if(self.totalarr.count == )
{
[self.totalarr addObject:str];
}else
{
if ([self.totalarr containsObject:str]) {
}else
{
[self.totalarr addObject:str];
}
} [self select:sender withselectmutablearray:self.drinksselects andarray:self.drinksbtns]; if(self.drinksselects.count == )
{
[self.totalarr removeObject:str];
} } //甜点类点击事件
- (IBAction)dessertClicked:(UIButton *)sender { NSString *str = @"甜点"; if(self.totalarr.count == )
{
[self.totalarr addObject:str];
}else
{
if ([self.totalarr containsObject:str]) {
}else
{
[self.totalarr addObject:str];
}
} [self select:sender withselectmutablearray:self.dessertselects andarray:self.dessertbtns]; if(self.dessertselects.count == )
{
[self.totalarr removeObject:str];
} }
很高兴。这是自己写的第一篇小功能小技术的实现。
以后会多多写点自己实现的小技术效能。望大家多多支持
谢谢;