Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2011, 12:20 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 9
NSpehler is on a distinguished road
Talking How do I use If-Else statements to change a UIImage

Hey folks!

I'm having an issue on my iPhone application.

I want my UIImage to change if a user press on it. I know how to do that :

Code:
- (IBAction)buttonClicked:(id)sender {
		UIImage *img = [UIImage imageNamed:@"Version.png"];
		[sender setImage:img forState:UIControlStateNormal];
}
But, my problem is that I'd like my UIImage to change back to the first image if we already pressed on the UIImage.

Here is my code :
Code:
- (IBAction)buttonClicked:(id)sender {
	
	if ([UIImage imageNamed:@"Version.png"]) 
        {
		UIImage *img = [UIImage imageNamed:@"VersionTouched.png"];
		[sender setImage:img forState:UIControlStateNormal];
	}
	else if ( [UIImage imageNamed:@"VersionTouched.png"]) 
        {
		UIImage *img = [UIImage imageNamed:@"Version.png"];
		[sender setImage:img forState:UIControlStateNormal];
	}
}
Hope you can edit my If-Else statements in order to be able to do that...

Thanks!
NSpehler is offline   Reply With Quote
Old 01-15-2011, 01:28 PM   #2 (permalink)
Registered Member
 
Lswpb's Avatar
 
Join Date: May 2010
Location: Europe
Posts: 41
Lswpb is on a distinguished road
Default

define your UIImage img in your .h resource file.

then you should use the following:

Code:
- (IBAction)buttonClicked:(id)sender {
	
	if (img == [UIImage imageNamed:@"Version.png"]) 
        {
		img = [UIImage imageNamed:@"VersionTouched.png"];
		[sender setImage:img forState:UIControlStateNormal];
	}
	else if (img == [UIImage imageNamed:@"VersionTouched.png"]) 
        {
		img = [UIImage imageNamed:@"Version.png"];
		[sender setImage:img forState:UIControlStateNormal];
	}
}
Please tell me if something is unclear or not working.
__________________
Please check out my apps:
-----------------------------------------------


-----------------------------------------------
Lswpb is offline   Reply With Quote
Old 01-15-2011, 02:03 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 9
NSpehler is on a distinguished road
Default

Thanks for your answer,
Unfortunatly, it's not working...

Inside my .h file I have :

Code:
@interface MyViewController : TTViewController {
	IBOutlet UIImage *img;
}

- (IBAction)buttonClicked:(id)sender;
And your code in the .m...

In my nib file, the button is connected to 'buttonClicked:'...

Any idea ?

Last edited by NSpehler; 01-15-2011 at 02:30 PM.
NSpehler is offline   Reply With Quote
Old 01-15-2011, 02:52 PM   #4 (permalink)
Registered Member
 
Lswpb's Avatar
 
Join Date: May 2010
Location: Europe
Posts: 41
Lswpb is on a distinguished road
Default

Try to change the UIImage from your .h to an UIImageView

So you'll write this in your .h

Code:
@interface MyViewController : TTViewController {
	IBOutlet UIImageView *img;
}

- (IBAction)buttonClicked:(id)sender;
__________________
Please check out my apps:
-----------------------------------------------


-----------------------------------------------
Lswpb is offline   Reply With Quote
Old 01-15-2011, 03:04 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 9
NSpehler is on a distinguished road
Default

No luck...
It's not even updating from Version.png to VersionTouched.png.

Maybe the fact that my UIImage is in a Custom UIButton...?
NSpehler is offline   Reply With Quote
Old 01-15-2011, 03:21 PM   #6 (permalink)
Registered Member
 
wanx's Avatar
 
Join Date: Dec 2010
Posts: 74
wanx is on a distinguished road
Default

isnīt it

Code:
if (img.image == [UIImage imageNamed:@"Version.png"])
?

Correct me if im wrong :P
wanx is offline   Reply With Quote
Old 01-15-2011, 03:23 PM   #7 (permalink)
Registered Member
 
Lswpb's Avatar
 
Join Date: May 2010
Location: Europe
Posts: 41
Lswpb is on a distinguished road
Default

Ok I got it all working for you

ButtonChange.zip

Please tell me if something is unclear or if you have any questions.
__________________
Please check out my apps:
-----------------------------------------------


-----------------------------------------------
Lswpb is offline   Reply With Quote
Old 01-15-2011, 03:34 PM   #8 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 9
NSpehler is on a distinguished road
Default

Quote:
Originally Posted by Lswpb View Post
Ok I got it all working for you

ButtonChange.zip

Please tell me if something is unclear or if you have any questions.
OH MY GOD!

You're truly AMAZING !
Now working like a charm!

I didn't know how to thank you enough so I rated & comment on your app in the French App Store with 5 stars.

THANK YOU!!!!!
NSpehler is offline   Reply With Quote
Old 01-15-2011, 03:35 PM   #9 (permalink)
Registered Member
 
Lswpb's Avatar
 
Join Date: May 2010
Location: Europe
Posts: 41
Lswpb is on a distinguished road
Default

You're welcome
__________________
Please check out my apps:
-----------------------------------------------


-----------------------------------------------
Lswpb is offline   Reply With Quote
Reply

Bookmarks

Tags
if else, press, statements, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 366
10 members and 356 guests
7twenty7, blueorb, dre, iAppDeveloper, iGamesDev, Mah6447, Morrisone, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0