用数组创建的多个Button, 点击的时候只能选中一个
2020-01-06 本文已影响0人
努努Nunu
self.btnList = [[NSMutableArray alloc] init];
self.btnArray = @[@"实时管控",@"日调节",@"周调度",@"其他"];
self.selectedBtn = nil;
NSInteger count = self.btnArray.count;
CGFloat InfoButtonwidth = (RkScreenWidth-40)/count;
[self.btnArray enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
MyMenuButton *infoBtn = [[MyMenuButton alloc] initWithFrame:CGRectMake(InfoButtonwidth*(idx%count)+30, 40, InfoButtonwidth, 20)];
[infoBtn setTitle:self.btnArray[idx] forState:UIControlStateNormal];
[infoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[infoBtn setImage:[UIImage imageNamed:@"noticeNoSelect"] forState:UIControlStateNormal];
[infoBtn setImage:[UIImage imageNamed:@"noticeSelect"] forState:UIControlStateSelected];
infoBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
infoBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
[infoBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:2];
infoBtn.tag = 100+idx;
[self addSubview:infoBtn];
[infoBtn addTarget:self action:@selector(infoBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.btnList addObject:infoBtn];
}];
((MyMenuButton *)[self.btnList objectAtIndex:0]).selected=YES;
- (void)infoBtnAction:(MyMenuButton *)sender {
((MyMenuButton *)[self.btnList objectAtIndex:0]).selected=NO;
if (sender != self.selectedBtn) {
self.selectedBtn.selected = NO;
sender.selected = YES;
self.selectedBtn = sender;
}else{
self.selectedBtn.selected = YES;
}
if (sender.tag == 100) {
} else if (sender.tag == 101) {
}
}
效果图:
截屏2020-04-21下午5.26.50.png