以编程方式更改(而不是初始化)UIBarButtonItem 标识符?

发布于 2024-09-06 01:40:20 字数 239 浏览 8 评论 0原文

在 IB 中,我可以将 UIBarButtonItem 的标识符设置为“play”,这会添加播放按钮的图像(右指三角形)。

有没有办法以编程方式更改此图像?我想在按下播放按钮时将其更改为“暂停”。

我知道您可以使用标识符初始化 UIBarButtonItem,但我还没有找到在初始化后更改它的方法。这可能吗?

我唯一能想到的就是删除旧按钮并在其位置初始化一个新按钮,但这似乎效率很低。

有什么想法吗?

In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).

Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.

I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?

The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.

Any thoughts?

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

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

发布评论

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

评论(3

英雄似剑 2024-09-13 01:40:20

好吧,我已经用谷歌搜索了这个问题,并遇到了 来自的示例代码Apple 他们做完全相同的事情(在工具栏按钮上切换播放/暂停按钮图形)。但他们没有使用 UIBarButtonItem 的内置播放和暂停标识符,而是使用自定义 UIButton 并切换自定义图像。

因此,如果苹果在 UIButton 上创建和切换自定义图像而不是内置的播放和暂停 UIBarButtonItem 按钮上遇到麻烦,那么我认为可以肯定地说,没有办法以编程方式更改一个 UIBarButtonItem。

这就是他们(Apple)在按下按钮时切换图像的方法:

// Call this when the button you want to toggle is pressed:
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];

将 p.playing 替换为您想要保持按钮状态的任何 BOOL 。 playButton 是工具栏中的自定义UIButtonpauseBtnBGplayBtnBG 是要切换的图像。

Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of UIBarButtonItem they use a custom UIButton and toggle custom images.

So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause UIBarButtonItem buttons then I think it's pretty safe to say there's no way to programatically change the identifier of a UIBarButtonItem.

This is what they (Apple) do to toggle the images when the button is pressed:

// Call this when the button you want to toggle is pressed:
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];

Replace p.playing with whatever BOOL you want to hold the state of your button. playButton is the custom UIButton in the toolbar. pauseBtnBG and playBtnBG are the images to toggle.

梨涡少年 2024-09-13 01:40:20

这似乎工作得相当好:

UIBarButtonItem *oldButton = [myToolBar.items objectAtIndex:1];
[myToolBar setItems:[NSArray arrayWithObjects:[myToolBar objectAtIndex:0], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(tapStopRefreshButton:)],nil] animated:NO];
[oldButton release];

在这个例子中,我有一个 UIWebView 的工具栏,当有人单击“重新加载”时,我希望它更改为“停止”。工具栏上只有一个灵活的空间和一个按钮 - 右对齐按钮 - 所以我抓住了对旧按钮的引用,使用与旧按钮相同的选择器创建了一个新按钮,重置选项卡栏上的按钮,然后松开原来的按钮。

不是最漂亮的,但您可以使用所有标准按钮,而无需覆盖按钮类。

This seems to work fairly well:

UIBarButtonItem *oldButton = [myToolBar.items objectAtIndex:1];
[myToolBar setItems:[NSArray arrayWithObjects:[myToolBar objectAtIndex:0], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(tapStopRefreshButton:)],nil] animated:NO];
[oldButton release];

In this example I had a toolbar for a UIWebView and when someone clicked Reload I wanted it to change to Stop. The toolbar only had a flexible space and the one button on it - to right-align the button - so I grabbed a reference to the old button, made a new one with the same selector as the old, reset the buttons on the tab bar, and then released the original button.

Not the prettiest, but you can use all standard buttons without having to override the button class(es).

云裳 2024-09-13 01:40:20

两个堆叠的工具栏怎么样?然后,您可以将一些系统按钮放在顶部,将其他按钮放在底部。如果按下播放按钮,则只需隐藏顶部工具栏即可。

What about 2 stacked toolbars? Then you can have some system buttons in the top one, and others in the bottom one. If the play button is pressed, then just hide the top toolbar.

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