第一,定义一个全局的按钮变量

@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;

    }

}

 

posted on 2017-11-28 21:27  i兮兮  阅读(608)  评论(0编辑  收藏  举报