how to load a .png image into round rect button?
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.
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.
[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.
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.
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.
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.
myButton.hidden =FALSE;
Slow down and read the docs when you get stuck. UIButton doc has tons of useful stuff in it.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Unable to load textures | wademcgillis | 15 | 6,197 |
Apr 9, 2010 03:37 PM Last Post: Macmenace |
|
| Increase skill level button | dunhill68 | 12 | 4,521 |
Jan 8, 2010 02:57 PM Last Post: dunhill68 |
|
| How do you load PNGs on a C++ program? | riruilo | 8 | 4,941 |
Oct 20, 2009 05:20 PM Last Post: miketucker |
|
| Saving the game state when pressing the Home button | Nacho | 3 | 3,002 |
Feb 10, 2009 05:33 PM Last Post: Nacho |
|
| curiosity button question | imaumac | 6 | 2,906 |
Jan 27, 2009 04:02 PM Last Post: imaumac |
|

