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 02-09-2012, 09:07 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 21
learningtocode is on a distinguished road
Default Change UIImageViews image from UIButton

I have been unsuccessful in the finding this answers. What is the best way to change a UIImageView's image that is on ViewController 1, from UIButtons from ViewController2?
learningtocode is offline   Reply With Quote
Old 02-09-2012, 11:20 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 94
JamesCahall is on a distinguished road
Default

Quote:
Originally Posted by learningtocode View Post
I have been unsuccessful in the finding this answers. What is the best way to change a UIImageView's image that is on ViewController 1, from UIButtons from ViewController2?
There are several ways to do this. Here is one.

On viewcontroller 1, create an observer in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToRun) name:@"RunThisMethod" object:nil];

Note: Be sure to add this to dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"RunThisMethod" object:nil];

Add the method to change the image:

- (void)methodToRun {
self.someImageView.image = [UIImage imageNamed:@"New_Image.jpg"];
}

On view controller 2, add the postNotification call to the button action:

[button addTarget:self action:@selector(sendPost) forControlEvents:UIControlEventTouchUpInside];

Add the method sendPost:

- (void)sendPost {
[[NSNotificationCenter defaultCenter] postNotificationName:@"RunThisMethod" object:nil];
}

That's it. If you need the new image to be variable, change methodToRun to methodToRunNSNotification *)notification and send a userInfo dictionary with the postNotification that has the image string.

Alternatively, you could store the image string in a singleton that is written by one, and read by the other. A notification would not be necessary if you changed the image in viewWillAppear of view controller 1 based upon the image string stored in the singleton.

Hope that helps.
JamesCahall is offline   Reply With Quote
Old 02-09-2012, 11:30 PM   #3 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Unless lots of objects need to know about the changed image, notifications are not the way to go. You should create a property on view controller 1 that you set from view controller 2.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 02-09-2012, 11:44 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 21
learningtocode is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Unless lots of objects need to know about the changed image, notifications are not the way to go. You should create a property on view controller 1 that you set from view controller 2.
That sounds like the direction I need to go. Do you know a good resource to learn how to do this? Thank you guys for your quick responses!!
learningtocode is offline   Reply With Quote
Old 02-10-2012, 12:09 AM   #5 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

http://www.iphonedevsdk.com/forum/ip...roperties.html
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Reply

Bookmarks

Tags
uibutton uiimageview

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: 395
12 members and 383 guests
chiataytuday, chits12345, ChrisYates, fiftysixty, fredidf, gmarro, KennyChong, kilobytedump, Leslie80, Meoz, ryantcb, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

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