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

编程入门 行业动态 更新时间:2024-10-24 04:30:15
本文介绍了如何以编程方式获取UIBarButtonItems的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于像UIButton这样的UIControl,您可以使用类似 myControl.state 以确定控件当前是否处于按下状态.

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.

但是,我需要对某些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的状态?

本文发布于:2023-11-26 00:08:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   方式   UIBarButtonItems

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!