Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aking63

macrumors newbie
Original poster
Mar 4, 2011
7
0
Toronto, On, Canada
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:

Code:
    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:

Code:
- (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
 
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
Code:
[buttonA setSelected:(!buttonB.selected)]

- Olaf
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.