button的单选点击事件
2016-12-13 本文已影响31人
大牛大神
1,创建一堆button 每一个button设置一个tag 123456.
2.所有的button共用一个点击事件 并且把button传进来
[button addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchCancel];
(ControlEvents不需要跟我一样);
3完成button的点击事件
-(void)selectedButton:(UIButton *)sender{
for (int i = 1; i<6; i++) {
if (sender.tag == i) {
sender.selected = YES;
sender.backgroundColor = BLUE;
continue;
}
UIButton * but = (UIButton*)[self.view viewWithTag:i];
but.selected = NO;
but.backgroundColor = GRAY;
}
}
(blue 和gray 是的宏的两个颜色 你可以随意替换);