利用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]];
? ? }
}