views:

62

answers:

2

I am trying to learn something basic and very important to develop decent iPhone apps. But I don't know enough to understand what I am not getting. Here's the question:

I have a window project, with 2 views - View1, View2. On each view (thru IB) I dropped an imageView control. When I call a function in view1 I want to set the image control (show an image) of View2.

How do I do that?

There must be a simple way but I haven't managed (and did search a lot) to find a straightforward simple way to do it or at least understand the concept.

Thanks in advance. -mE

A: 

You should connect the UIImageViews in IB to outlets in your ViewController class. In your class's .h interface file, add as appropriate:

IBOutlet UIImageView *view1;

add a property:

@property (nonatomic,retain) IBOutlet UIImageView *view1;

and in your class implementation file (.m), synthesize the property:

@synthesize view1;

Finally, you need to connect the UIImageView object in IB to that new outlet. Then you can access the image property of the UIView within your class with view1.image

wkw
my question is not how do I access at the UIViewImage from one viewcontroller (VC). It's how do I access from one VC to another object like and ImageView located in other VC. What you're describing is how to connect a control which I have already figure out.
amok
+1  A: 

You don't necessarily need to share the image. Is it large? Are you downloading it? In other words, does it really need to be shared?

If it does, you can just instantiate the UIImage in your app delegate and pass it to each view controller when they are created. Set a property for each view controller for the image. Something like this:

ViewController1 *controller1 = [[ViewController1 alloc init];
[controller1 setImage:image];
[[self navigationController] pushViewController:controller1];
[controller1 release];

ViewController2 *controller2 = [[ViewController2 alloc init];
[controller2 setImage:image];
[[self navigationController] pushViewController:controller2];
[controller2 release];

Then in each view controller's viewDidLoad, you can set the image for your imageView's

- (void)viewDidLoad;
{
    [super viewDidLoad];
    [[self imageView] setImage:[self image]];
}

I'm not sure how you're a loading your view controllers, but the above code assumes you are using a navigation controller stack. If you need clarification for a different approach, let me know.

Matt Long
the image is saved locally and basically on one view I show a tiny one and on the second I expand it for a better visualization. Thank you this does answer to my question - the key is put in the delegate (I don't want to create a singleton just for this)
amok