问题描述
对于像UIButton
这样的UIControl
,您可以使用类似myControl.state
以确定控件当前是否处于按下状态.
With a UIControl
such as a UIButton
you can use something likemyControl.state
to figure out whether the control is currently being pressed down.
但是,我需要对某些UIBarButtonItems
(不是从UIControl
派生的)进行同样的操作,以便在按下其中一个时可以停止编辑表.
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.
这是我的代码:
- (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
,但是我该怎么做呢?
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?
推荐答案
如果您想更好地控制UIBarButtonItems
,最好的办法是将它们重新创建为UIButtons
(使用自定义插图等),然后使用UIBarButtonItem
的-initWithCustomView
从实际的UIViews创建按钮项.
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.
这篇关于如何以编程方式获取UIBarButtonItems的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!