UIBarButtonItem :选择/取消选择一个项目

发布于 2024-08-21 21:43:41 字数 173 浏览 9 评论 0原文

我有一个最喜欢的图像要显示在工具栏上的 UIBarButtonItem 上。

当未选择/选择此项时如何更改它?像这样的屏幕截图:

在此处输入图像描述

谢谢!

I have a favorite image to display on a UIBarButtonItem, on an toolbar.

How do you do to change it when this item is unselected/selected? like this screenshot:

enter image description here

Thanks!

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

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

发布评论

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

评论(2

萌酱 2024-08-28 21:43:41

您可以使用 UIBarButtonItems 创建两个数组:一个包含第一个图像,一个包含第二个图像。像这样:

// array with unselected
UIBarButtonItem *unselectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_unselected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithUnselected = [NSArray arrayWithObject:unselectedItem]; // declared as NSArray*
[unselectedItem release];

// array with selected
UIBarButtonItem *selectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_selected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithSelected = [NSArray arrayWithObject:selectedItem]; // declared as NSArray*
[selectedItem release];

然后在两组工具栏项目之间切换:

toolbar.items = self.itemsWithSelected; // or self.itemsWithUnselected

如果工具栏上不止一个按钮,则只需将其余项目添加到两个数组中即可。

You can make two arrays with UIBarButtonItems: one with the first image and one with the second image. Like this:

// array with unselected
UIBarButtonItem *unselectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_unselected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithUnselected = [NSArray arrayWithObject:unselectedItem]; // declared as NSArray*
[unselectedItem release];

// array with selected
UIBarButtonItem *selectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_selected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithSelected = [NSArray arrayWithObject:selectedItem]; // declared as NSArray*
[selectedItem release];

and then switch between the two sets of toolbar items with:

toolbar.items = self.itemsWithSelected; // or self.itemsWithUnselected

If you have more than just the one button on your toolbar then just add the rest of the items to both arrays.

总攻大人 2024-08-28 21:43:41
// First create UIButton object
UIButton *btnCustom = [UIButton buttonWithType:UIButtonTypeCustom];

// Set Frame because without frame your button can not be shown on navigation bar
[btnCustom setFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];

// Set unselected image
[btnCustom setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME_UNSELECTED"] forState:UIControlStateNormal];

// set selected image
[btnCustom setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME_SELECTED"] forState:UIControlStateSelected];

// set action method
[btnCustom addTarget:self action:@selector(btnCustom_click:) forControlEvents:UIControlEventTouchUpInside];


UIBarButtonItem *btnCustomBar = [[UIBarButtonItem alloc]initWithCustomView:btnCustom];

[self.navigationItem setRightBarButtonItem:btnCustomBar];

// action Method

- (IBAction)btnCustom_click:(id)sender
{
   if(![sender isSelected])
       [sender setSelected:YES];
   else
       [sender setSelected:NO];
}
// First create UIButton object
UIButton *btnCustom = [UIButton buttonWithType:UIButtonTypeCustom];

// Set Frame because without frame your button can not be shown on navigation bar
[btnCustom setFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];

// Set unselected image
[btnCustom setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME_UNSELECTED"] forState:UIControlStateNormal];

// set selected image
[btnCustom setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME_SELECTED"] forState:UIControlStateSelected];

// set action method
[btnCustom addTarget:self action:@selector(btnCustom_click:) forControlEvents:UIControlEventTouchUpInside];


UIBarButtonItem *btnCustomBar = [[UIBarButtonItem alloc]initWithCustomView:btnCustom];

[self.navigationItem setRightBarButtonItem:btnCustomBar];

// action Method

- (IBAction)btnCustom_click:(id)sender
{
   if(![sender isSelected])
       [sender setSelected:YES];
   else
       [sender setSelected:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文