简体   繁体   中英

change UIButton state selected of all the button in UIToolBar

I have four UIButton s 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];
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM