问题
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