iphone - UIToolBar 选中按钮状态

标签 iphone objective-c xcode ios uitoolbar

我有一个 UItableView,底部有一个工具栏。工具栏有两个按钮,分别是“水果”和“蔬菜”。单击它们将仅显示相应的类别。

我已经设法让显示功能正常工作,但我不知道如何在选择时将按钮设置为 selectedState。

我创建了如下所示的工具栏

UIBarButtonItem *fruits = [[UIBarButtonItem alloc] initWithTitle:@"Fruits" style:UIBarButtonItemStyleBordered target:self action:@selector(fruitsClicked:)];
UIBarButtonItem *vegetables = [[UIBarButtonItem alloc] initWithTitle:@"Vegetables" style:UIBarButtonItemStyleBordered target:self action:@selector(vegetablesClicked:)];
NSArray *items = [NSArray arrayWithObjects: fruits,vegetables,nil];
[fruits release];
[vegetables release];
[self setToolBarItems:items];

和像

这样的选择器
-(void)fruitsClicked:(id)sender{
     //code here
}

由于我之前松开按钮,所以无法设置

fruits.enabled=NO;

如有任何帮助,我们将不胜感激。

最佳答案

我认为添加单独的按钮来做到这一点不是一个好的设计和策略,您应该使用 UISegmentad 控件,它是您想要的组件设计:在 X 中选择一个段。

你可以像这样使用它(例如选择第一项):

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array];
[segment setSelectedSegmentIndex:0];

并获取所选项目:

segment.selectedSegmentIndex

关于iphone - UIToolBar 选中按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5353490/

相关文章:

iphone - 设置UILocalNotification的applicationIconBadgeNumber

ios - 使用音频队列 iOS api 将最后 30 秒的音频保存在文件中

iphone - Titanium 与原生工具

objective-c - 为什么 `__NSArrayI` 不是一种 `[NSArray class]?

iphone - 请将此设备连接到 Xcode 以安装开发支持文件

iPhone OpenGL fps 测试

ios - 带有 NavigationBarHidden 的导航 Controller

ios - 不兼容的整数到指针转换 "int"到 "id"的参数类型

objective-c - 为什么我在正确链接框架时会收到 "_OBJC_CLASS_$..., referenced from:"链接器错误?

swift - 如何在 MapView 上绘制两个 MKPolygon,而不让它们连接?