iDevGames Forums
how to load a .png image into round rect button? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: how to load a .png image into round rect button? (/thread-1943.html)



how to load a .png image into round rect button? - ericnk - Jan 7, 2009 11:35 PM

I am attempting to load an image (.png) into a round rect button ( programatically ). Let say I have a button defined via interface builder and a connection to an IBAction called buttonclicked. I would like the method buttonclicked to load or change the image for the button. Can anyone give me the exact code to do this? I havent been able to get it to work.


how to load a .png image into round rect button? - kodex - Jan 7, 2009 11:55 PM

Two options depending on whether you mean the background image or the button icon

[myButton setBackgroundImage: [UIImage imageNamed: @"background.png"] forState: UIControlStateHighlighted];

or

[myButton setImage: [UIImage imageNamed: @"icon.png"] forState: UIControlStateHighlighted];

UIControlStateHighlighted
Highlighted state of a control. A control enters this state when a touch enters and exits during tracking and and when there is a touch up. You can retrieve and set this value through the highlighted property.

Available in iPhone OS 2.0 and later.

Declared in UIControl.h.

If you are calling this from an IBAction you can replace "myButton" with sender.


how to load a .png image into round rect button? - ericnk - Jan 8, 2009 06:41 AM

Thanks Kodex! With that information I was able to get it to work.

here is what I had ( didnt work ):
[sender setBackgroundImage: [ UIImage imageNamed: @"ball.png"];

and after reading your post I changed it to ( works!):
[ sender setImage: [UIImage imageNamed: @"ball.png"] forState: UIControlStateNormal];

I am not clear on the difference between background image and icon image ( button image?). If you have time I would like to see the code to remove the button from the window, as well as how to make it reappear.
I have been trying to pull code out of "the iphone developers cookbook" but with my limited knowledge of objective-c, I usually get into trouble.


how to load a .png image into round rect button? - kodex - Jan 8, 2009 09:54 AM

myButton.hidden = TRUE;

myButton.hidden =FALSE;

Slow down and read the docs when you get stuck. UIButton doc has tons of useful stuff in it.