关于button的单选多选问题

button的单选、多选问题,其实是基于button的Selected属性来做的简单设置。虽然很简单,还是整理一下方便查阅;

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIButton * btn;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic,strong) UIView * lineView;
@property (nonatomic,weak) UIButton *Selectbutton;
@end

@implementation ViewController
- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray arrayWithCapacity:0];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [btn setTitle:[NSString stringWithFormat:@"%d", i + (3 * j) + 1] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
            btn.tag = (i + (3 * j) + 1) + 100;
            [btn addTarget:self action:@selector(clickedBtnWith:) forControlEvents:UIControlEventTouchUpInside];
            btn.frame = CGRectMake(20 + i * 120, 20 + j * 70, 100, 50);
            if (i==0 && j==0) {
                btn.selected = YES;
                btn.backgroundColor = [UIColor grayColor];
                self.Selectbutton = btn;
            }
            [self.view addSubview:btn];
            _btn = btn;
        }
    }
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(20, 72, 100, 1)];
    self.lineView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.lineView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)clickedBtnWith:(UIButton *)btn{
    NSLog(@"selected===%d",btn.selected);
//    //多选
//        if (!btn.selected) {
//            [btn setBackgroundColor:[UIColor brownColor]];
//            [self.array addObject:[NSNumber numberWithInteger:btn.tag - 100]];
//        }else{
//            [btn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
//            if ([self.array containsObject:[NSNumber numberWithInteger:btn.tag - 100]]) {
//                [self.array removeObject:[NSNumber numberWithInteger:btn.tag - 100]];
//            }
//        }
//        btn.selected = !btn.selected;
//    NSLog(@"%@", _array.description);
   
   
//单选
   
    NSLog(@"点击了第%ld 个按钮", (long)btn.tag - 100);
    if (!btn.isSelected) {
       
        self.Selectbutton.selected = !self.Selectbutton.selected;
       
        self.Selectbutton.backgroundColor = [UIColor groupTableViewBackgroundColor];
       
        btn.selected = !btn.selected;
       
        btn.backgroundColor = [UIColor grayColor];
       
        self.Selectbutton = btn;
       
    }

}

//  颜色转换为背景图片

- (UIImage *)imageWithColor:(UIColor *)color

{
   
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
   
    UIGraphicsBeginImageContext(rect.size);
   
    CGContextRef context = UIGraphicsGetCurrentContext();
   
   
   
    CGContextSetFillColorWithColor(context, [color CGColor]);
   
    CGContextFillRect(context, rect);
   
   
   
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   
    UIGraphicsEndImageContext();
   
   
   
    return image;
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

你可能感兴趣的:(关于button的单选多选问题)