how do i only allow one selected button at once?

Discussion in 'iOS Programming' started by aking63, Jun 2, 2011.

  1. aking63 macrumors newbie

    Mar 4, 2011
    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:

        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
  2. forum user macrumors regular

    Aug 28, 2008
    "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

Share This Page