croigsalvador
croigsalvador

Reputation: 2013

change UIButton state selected of all the button in UIToolBar

I have four UIButtons added to a custom UIToolBar, I can change the selected state of the pressed button when I press in it and call the action function. The problem is that I do not know how to get the rest of buttons and change their selected property. Is this easier with segmentedControl?

As you can see in the picture, when one of them is selected I do not change the rest of the states..

I do that with this code :

if([sender tag] == i){
    if([sender isSelected]){
        NSLog(@"selected");
        [sender setSelected:NO];
    } else {
        NSLog(@"No selected");
        [sender setSelected:YES];
    }

UIToolBar

I have tried this, but it makes an error:

for( int i = 0; i < 4 ; i++){
    if([sender tag] == i){
        if([sender isSelected]){
            NSLog(@"selected");
            [sender setSelected:NO];
        } else {
            NSLog(@"No selected");
            [sender setSelected:YES];
        }
    } else {
        UIButton *btn = (UIButton *)[self viewWithTag:i];
        if([btn isSelected]){
            NSLog(@"selected");
            [sender setSelected:NO];
        } else {
            NSLog(@"No selected");
            [sender setSelected:YES];
        }
    }
}

Upvotes: 0

Views: 1085

Answers (1)

David Hoerl
David Hoerl

Reputation: 41622

You ask the toolbar for the array of its items using the items message. What you get back is a list of UIBarButtonItems. You can then query each of those items as to its state. Assuming the UIBarButtonItem has a custom view, you would get that, verify that its class is a UIButton, then message it to get or set state.

Upvotes: 1

Related Questions