UIToolBar选中按钮状态

发布于 2024-10-24 01:35:15 字数 823 浏览 4 评论 0原文

我有一个 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;

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

I have a UItableView with a toolbar in the bottom. The toolbar has two buttons say 'fruits','vegetables'. Clicking on them will display only the corresponding category.

I have managed to get the displaying feature working, but i do not know how to set the button in selectedState when chosen.

I created the toolbar as shown below

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];

and selectors like

-(void)fruitsClicked:(id)sender{
     //code here
}

Since i release the buttons earlier, i am not able to set

fruits.enabled=NO;

ANy help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫七 2024-10-31 01:35:15

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

您可以像这样使用它(选择第一个) item 例如):

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

并获取所选项目:

segment.selectedSegmentIndex

I think adding separate Buttons to do that is not a good design and strategy, you should use a UISegmentad control which is a component design for what you want: have one segment selected among X.

You can use it like this (to select the first item for example):

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

And to get the selected item:

segment.selectedSegmentIndex
情场扛把子 2024-10-31 01:35:15

传递给 FruitClicked 处理程序的 sender 参数将是指向 UIBarButtonItem 的指针。您可以像这样投射它:

[((UIBarButtonItem*)sender) setEnabled:NO];

但是,一旦按钮被禁用,它就不再接受点击(即您无法再次点击它来重新启用它)。我会为按钮使用 customView 并自己实现启用/禁用外观。

The sender argument passed to your fruitClicked handler will be a pointer to the UIBarButtonItem. You can cast it like this:

[((UIBarButtonItem*)sender) setEnabled:NO];

However, once the button is disable it no longer accepts taps (i.e. you couldn't tap it again to re-enable it). I would use a customView for the button and implement the enabled/disable appearance yourself.

栖迟 2024-10-31 01:35:15

我没有任何使用 UIToolBars 的经验,但据我所知,选择器获取了发送者的 ID,对吧?您可以使用它来访问单击的按钮,然后将其设置为选中状态。

I do not have any experience with UIToolBars, but from what i see, the selector gets the ID of the sender, right? You can use that to access the clicked button, and then set it selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文