View Full Version : how do i only allow one selected button at once?

Jun 3, 2011, 01:41 AM
how do i make these buttons so that only one can be used at a time? Im not getting any errors right now when i run btw. Im just looking for a solution to my challenge. Thanks for any help

they are generated in a for loop like this:

for (int l=0; l<list.length; l++) {

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;

[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:@" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];

And then the action for when a button is clicked is:

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

int tag = sender.tag;

if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];

right now everything works but i want it to know if theres already a button selected and deselect that other button, or else to automatically deselect any time the user clicks outside of the range of that button

thanks in advance

forum user
Jun 3, 2011, 05:19 AM
how do i make these buttons so that only one can be used at a time?

"Can be used" translates to button.enabled not to button.selected. If you want to de-select button A when button B is pressed then you would do something like [buttonA setSelected:(!buttonB.selected)]

- Olaf