how to load a .png image into round rect button?

Apprentice
Posts: 13
Joined: 2008.09
Post: #1
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.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
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.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2008.09
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #4
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.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Unable to load textures wademcgillis 15 7,765 Apr 9, 2010 03:37 PM
Last Post: Macmenace
  Increase skill level button dunhill68 12 5,473 Jan 8, 2010 02:57 PM
Last Post: dunhill68
  How do you load PNGs on a C++ program? riruilo 8 6,255 Oct 20, 2009 05:20 PM
Last Post: miketucker
  Saving the game state when pressing the Home button Nacho 3 3,392 Feb 10, 2009 05:33 PM
Last Post: Nacho
  curiosity button question imaumac 6 3,237 Jan 27, 2009 04:02 PM
Last Post: imaumac