How do I programmatically get the state of UIBarBu

2019-08-07 06:31发布

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?

2条回答
Lonely孤独者°
2楼-- · 2019-08-07 06:55

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.

查看更多
疯言疯语
3楼-- · 2019-08-07 07:02

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.

查看更多
登录 后发表回答