按钮组单选某个按钮

来源:互联网 发布:如何安装网络 编辑:程序博客网 时间:2024/04/28 04:45

1.for循环创建button

@interface LoopButtonView ()// 按钮数组@property (nonatomic, strong) NSMutableArray *btnArray;// 选中按钮@property (nonatomic, strong) UIButton *selectedBtn;@end@implementation LoopButtonView-(instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        self.btnArray = [NSMutableArray array];    }    return self;}- (void)addOptionBtn:(NSArray *)btnArr{    // 1.删掉之前的所有按钮    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];    // 2.添加新的待选按钮    int count = (int) btnArr.count;    int totalColumns = 7;//修改此值排列按钮有几列    int totalRow;//总行数    if (count % totalColumns != 0) {        totalRow = count/totalColumns+1;    }else{        totalRow = count/totalColumns;    }        for (int i = 0; i<count; i++) {        // 1.取模型数据//        BtnParamsModel *model = [BtnParamsModel buttonInfoWithDict:btnArr[i]];        // 2.创建按钮        UIButton *optionBtn = [[UIButton alloc] init];                // 3.frame        CGFloat optionW = 25.0f;        CGFloat optionH = optionW;        // 控制器view的宽高        CGFloat viewW = self.frame.size.width;        CGFloat viewH = self.frame.size.height;                int col = i % totalColumns;        int row = i / totalColumns;                //列间距        CGFloat colmargin = (viewW-optionW*totalColumns)/(totalColumns+1);        CGFloat optionX = colmargin + col * (optionW + colmargin);                //行间距        CGFloat topMargin = (viewH - optionH *totalRow)/(totalRow+1);        CGFloat optionY = topMargin + row * (optionH + topMargin);                optionBtn.frame = CGRectMake(optionX, optionY, optionW, optionH);        //4.设置内容        [optionBtn setTitle:btnArr[i] forState:UIControlStateNormal];                        //文字颜色        optionBtn.titleLabel.font = [UIFont systemFontOfSize:12];        [optionBtn setTitleColor: [UIColor grayColor]  forState:UIControlStateNormal];                optionBtn.layer.borderColor = [UIColor cyanColor].CGColor;        optionBtn.layer.borderWidth =1.0f;        optionBtn.backgroundColor = [UIColor whiteColor];                //圆角        optionBtn.layer.cornerRadius = 8;                //5.添加        [self addSubview:optionBtn];        //阴影        optionBtn.layer.shadowColor = [UIColor blackColor].CGColor;        optionBtn.layer.shadowOffset = CGSizeMake(0.5, 0.5);        optionBtn.layer.shadowOpacity = 0.4;        optionBtn.layer.shadowRadius = 3;                //6.监听点击        [optionBtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];                optionBtn.tag = i;        [self.btnArray addObject:optionBtn];    }}//单选按钮处理-(void)optionClick:(UIButton *)sender{    NSLog(@"点击了%@", sender.titleLabel.text);        self.selectedBtn = sender;        sender.selected = !sender.selected;        for (NSInteger j = 0; j < [self.btnArray count]; j++) {        UIButton *btn = self.btnArray[j] ;        if (sender.tag == j) {            btn.selected = sender.selected;        } else {            btn.selected = NO;        }        btn.backgroundColor = [UIColor whiteColor];    }        UIButton *btn = self.btnArray[sender.tag];    if (btn.selected) {        btn.backgroundColor = [UIColor redColor];    } else {        btn.backgroundColor = [UIColor whiteColor];    }                if (self.delegate && [self.delegate respondsToSelector:@selector(loopButtonViewDelegateWithBtnClicked:)]) {        [self.delegate loopButtonViewDelegateWithBtnClicked:sender];    }}


2.controller类添加view

 LoopButtonView   * loopView = [[LoopButtonView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];    NSArray * array = @[@"29",@"30",@"31",@"01",@"02",@"03",@"04",@"29",@"30",@"31",@"01",@"02",@"03",@"04"];    [loopView addOptionBtn:array];    [self.view addSubview:loopView];


3.注意几点

a.存放按钮的数组需要初始化。

b.tag值要设置,以便viewcontroller内的点击事件的使用。


4.参考demo链接

http://download.csdn.net/detail/sunnysu99/9921465

原创粉丝点击