您的位置:首页 > 其它

一组按钮,其中一个选中,其余取消选中效果

2017-11-28 21:27 260 查看
第一,定义一个全局的按钮变量

@property (nonatomic, strong) UIButton *selectedBtn;

第二,循环创建按钮

//好评中评差评按钮

_normalArr = [NSArray arrayWithObjects:@"goodBtn_normal",@"middleBtn_normal",@"badBtn_normal", nil];

_selectArr = [NSArray arrayWithObjects:@"goodBtn_select",@"middleBtn_select",@"badBtn_select", nil];

for (int i = 0; i<3; i++) {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.tag = i;

button.frame = CGRectMake(60 + (SCREEN_WIDTH - 80)/ 3 * i, 10, (SCREEN_WIDTH - 80)/ 3, 40);

[button addTarget:self action:@selector(doButtonAction:) forControlEvents:UIControlEventTouchUpInside];

[button setImage:[UIImage imageNamed:_normalArr[i]] forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:_selectArr[i]] forState:UIControlStateSelected];

//将循环创建的button都添加到view上面

[_opinionRatingView addSubview:button];

}

第三,监听按钮的点击,切换选中按钮

#pragma mark - button触发的方法

#pragma mark - button触发的方法

-(void)doButtonAction:(UIButton *)sender {

if (sender != _selectedBtn) {

_selectedBtn.selected = NO;

sender.selected = YES;

_selectedBtn = sender;

}else{

_selectedBtn.selected = YES;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐