简体   繁体   中英

How do I programmatically get the state of UIBarButtonItems?

With a UIControl such as a UIButton you can use something like myControl.state to figure out whether the control is currently being pressed down.

However, I need to do the same with some UIBarButtonItems (which are not derived from UIControl ), so that I can stop my table from editing while one of them is pressed down.

Here's my code:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //other checks
    for(int b=0; b<self.toolbar.items.count; b++)
    {
        UIControl *currentControl= [self.toolbar.items objectAtIndex:b];
        if(currentControl.state==UIControlStateHighlighted)
        {
            return NO;
        }
    }
    return YES;
}

Obviously, it doesn't work, since it assumes that UIBarButtonItems can be treated as UIControls , but how would I do what I'm trying to do here?

If you want more control over your UIBarButtonItems the best thing to do is to recreate them as UIButtons (using custom art, etc), and then use the -initWithCustomView of UIBarButtonItem to create button items from actual UIViews.

This will give you full access to the usual button interactions methods: the only downside is you won't get the nice bar button style by default, and you'll have to provide the art for this yourself.

I had a similar problem before. I couldn't fix it, so I moved on. Here is what I did:

Use ToolBar instead of navigation bar, then use UIButton instead of UIBarButtonItem inside the toolbar.

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