利用Objective-C撸了个小玩意,先看一下效果。
多选 单选+去除以下是代码部分:
- (void)viewDidLoad {
[super viewDidLoad];
for(NSIntegeri =0; i <5; i++) {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(10,100+50*i +10*i,100,50);
[btnsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnsetTitle:[NSString stringWithFormat:@"%ld",i] forState:UIControlStateNormal];
btn.tag=1000+i;
[btnsetBackgroundColor:[UIColor orangeColor]];
[btnaddTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
}
//多选
- (void)btnAction:(UIButton *)sender{
sender.selected= !sender.isSelected;
if(sender.selected) {
NSLog(@"选中");
[sendersetBackgroundColor:[UIColor redColor]];
}else{
NSLog(@"未选中");
[sendersetBackgroundColor:[UIColor orangeColor]];
}
}
//单选和去除
- (void)btnAction:(UIButton *)sender{
sender.selected= !sender.isSelected;
for(NSIntegeri=0; i<5; i++) {
if(sender.tag== i+1000) {
if(sender.selected) {
[sendersetBackgroundColor:[UIColor redColor]];
}else{
[sendersetBackgroundColor:[UIColor orangeColor]];
}
continue;
}
UIButton *btn = (UIButton *)[self.view viewWithTag:i + 1000];
btn.selected=NO;
[btnsetBackgroundColor:[UIColor orangeColor]];
}
}
网友评论