button的單選、多選問題筒严,其實(shí)是基于button的Selected屬性來做的簡單設(shè)置。雖然很簡單情萤,還是整理一下方便查閱鸭蛙;
#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(@"點(diǎn)擊了第%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;
???????
??? }
}
//? 顏色轉(zhuǎn)換為背景圖片
- (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.
}