对于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(使用自定义插图等),然后使用-initWithCustomViewUIBarButtonItem从实际的UIViews创建按钮项。

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

关于ios - 如何以编程方式获取UIBarButtonItems的状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11209689/

10-13 08:48