How do I programmatically get the state of UIBarButtonItems?

浪尽此生 提交于 2020-01-05 15:16:51

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11209689/how-do-i-programmatically-get-the-state-of-uibarbuttonitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!