bkbeachlabs
bkbeachlabs

Reputation: 2171

how do I only allow one selected button at a time? / make button know if i click somewhere else

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

Upvotes: 1

Views: 4518

Answers (2)

kororo
kororo

Reputation: 2022

I would suggest to put all ur buttons to Array in your button initialisation

NSMutableArray* buttons = [NSMutableArray arrayWithCapacity: list.length];

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];
        [buttons addObject: aButton];
}

and every time buttonClicked triggered, then do your logic:

for (UIButton* button in buttons)
    {
        if (button != sender)
        {
             [button setSelected: FALSE];
             [button setBackgroundColor:[UIColor redColor]];
        }
    }

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]];
    }

hope helps :)

Upvotes: 5

Vladimir
Vladimir

Reputation: 170859

You can store currently selected button in a separate variable and deselect it in buttonClicked: method:

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

    int tag = sender.tag;

    currentButton.selected = NO;
    if (currentButton != sender){
       currentButton = sender;
       currentButton.selected = YES;
    }
    else{
       currentButton = nil;
    }
}

Also you can specify background colour for each state in button itself so you actually don't need to change it each time manually

If you also want to deselect your button when user just touches the screen you can implement touchesEnded:withEvent: in your view controller and reset currentButton in it (that method will get called if no other control intercept the touch event - so it may not be sufficient in all cases)

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
    currentButton.selected = NO;
    currentButton = nil;
}

Upvotes: 1

Related Questions