如何以编程方式获取UIBarButtonItems的状态?

使用UIControl (如UIButton您可以使用类似myControl.state来确定控件当前是否被按下。

但是,我需要对一些UIBarButtonItems (它们不是从UIControl派生)做同样的事情,这样我可以阻止我的表编辑,同时按下其中一个。

这是我的代码:

 - (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; } 

显然,它不起作用,因为它假设UIBarButtonItems 可以被视为UIControls ,但我怎么做我想在这里做的事情?

如果你想要更多地控制你的UIBarButtonItems ,最好的办法是将它们重新创建为UIButtons (使用自定义艺术等),然后使用UIBarButtonItem从实际的UIBarButtonItem创建按钮项。

这将使您可以完全访问常用的按钮交互方法:唯一的缺点是默认情况下您将无法获得漂亮的条形按钮样式,并且您必须自己为此提供艺术。

我之前遇到过类似的问题。 我无法解决它,所以我继续前进。 这是我做的:

使用ToolBar而不是导航栏,然后在工具栏中使用UIButton而不是UIBarButtonItem。