title: Passing image from one view to another
body: <p>I'm getting an image from the user from the photo gallery, and I want to pass it to a another view...</p>
  
  <p>I've read that I should be able to simply do something like this:</p>
  
  <pre><code>secView.imgView.image = selectedImage.image;
  </code></pre>
  
  <p>Setting my UIImageView on the second view to be the image from my UIImageView...
  But the UIImageView in the second view is just empty...</p>
  
  <p>My code is this...</p>
  
  <p>ViewController.h:</p>
  
  <pre><code>@interface ViewController : UIViewController &lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;
  {
      UIImagePickerController *picker;
      IBOutlet UIImageView *selectedImage;
  
      SecondView *secondView;
  }
  
  @property (nonatomic, retain) UIImageView *selectedImage;
  @property (nonatomic, retain) SecondView *secondView;
  
  -(IBAction)sendImage:(id)sender;
  -(IBAction)openGallery:(id)sender;
  
  @end
  </code></pre>
  
  <p>ViewController.m (only the relevant parts)</p>
  
  <pre><code>-(IBAction)sendImage:(id)sender
  {
      if(self.secondView == nil)
      {
          SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
  
          self.secondView = secView;
  
          secView.imgView.image = selectedImage.image;
      }
  
      [self.navigationController pushViewController:secondView animated:YES];
  }
  
  -(IBAction)openGallery:(id)sender
  {
      picker = [[UIImagePickerController alloc] init];
  
      picker.delegate = self;
  
      picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  
      [self presentModalViewController:picker animated:YES];
  }
  
  -(void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker 
  {    
      [Picker dismissModalViewControllerAnimated:YES];    
  }
  
  - (void)imagePickerController:(UIImagePickerController *) Picker
  
  didFinishPickingMediaWithInfo:(NSDictionary *)info 
  {    
      // Save the image
      selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
  
      [Picker dismissModalViewControllerAnimated:YES];
  }
  </code></pre>
  
  <p>In another post I read that another approach was to do this:</p>
  
  <pre><code>-(IBAction)sendImage:(id)sender
  {
      if(self.secondView == nil)
      {
          SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
  
          self.secondView = secView;
  
          [secView setImage:selectedImage.image];
      }
  
      [self.navigationController pushViewController:secondView animated:YES];
  }
  </code></pre>
  
  <p>and then in the secondview.h</p>
  
  <pre><code>-(void)setImage:(UIImage *)image;
  </code></pre>
  
  <p>and .m</p>
  
  <pre><code>-(void)setImage:(UIImage *)image
  {
      [imgView setImage:image];
  }
  </code></pre>
  
  <p>But both approaches results in an empty UIImageView...
  So what is the correct approach to doing this?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>SecondView is fairly simple at the moment... (in the posted code I try and follow method two)</p>
  
  <pre><code>@interface SecondView : UIViewController
  {
      UIImageView *imgView;
  }
  
  @property (nonatomic, retain) IBOutlet UIImageView *imgView;
  
  -(void)setImage:(UIImage *)image;
  
  @end
  
  @implementation SecondView
  
  @synthesize imgView;
  
  -(void)setImage:(UIImage *)image
  {
      [imgView setImage:image];
  }
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)didReceiveMemoryWarning
  {
      // Releases the view if it doesn't have a superview.
      [super didReceiveMemoryWarning];
  
      // Release any cached data, images, etc that aren't in use.
  }
  
  #pragma mark - View lifecycle
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view from its nib.
  }
  
  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
  }
  
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }
  
  @end
  </code></pre>
  
  <p><strong>SOLUTION</strong>
  Okay... Went back to the first method and made some alterations which ultimately worked...</p>
  
  <pre><code>-(IBAction)sendImage:(id)sender
  {
      if(self.secondView == nil)
      {
          SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
  
          self.secondView = secView;
  
          secView.theImage = selectedImage.image;
      }
  
      [self.navigationController pushViewController:secondView animated:YES];
  }
  </code></pre>
  
  <p>And in the secondView, I created UIImage *theImage - then I set [imgView setImage:theImage]; in viewDidLoad, which worked...</p>