views:

330

answers:

2

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 programatically? 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?

+1  A: 

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.

Nebs
A: 

Thanks so much for posting this answer. I was also struggling with this for a while and fake UIButtons did the trick. This is particularly useful for iPad projects, where the standard iPhone sized barbuttonitems are a little on the small side.

Wump
No problem. You might want to write comments like this by clicking 'add comment' under the answer instead of making it an answer itself. :)
Nebs