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;
任何帮助,我们将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为添加单独的按钮来做到这一点并不是一个好的设计和策略,您应该使用 UISegmentad 控件,它是您想要的组件设计:在 X 中选择一个段。
您可以像这样使用它(选择第一个) item 例如):
并获取所选项目:
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):
And to get the selected item:
传递给 FruitClicked 处理程序的 sender 参数将是指向 UIBarButtonItem 的指针。您可以像这样投射它:
但是,一旦按钮被禁用,它就不再接受点击(即您无法再次点击它来重新启用它)。我会为按钮使用 customView 并自己实现启用/禁用外观。
The sender argument passed to your fruitClicked handler will be a pointer to the UIBarButtonItem. You can cast it like this:
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.
我没有任何使用 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.