Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage and prepareforSegue doesn´t pass the image

I would like that appeared the Image on the second view with this but I can´t see any photo I don´t know why. I did this:

FirstClassViewController.m:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"ShowCover"]) {
            MovieImageViewController *photo = [segue destinationViewController];       
            photo.cover.image = [UIImage imageNamed:@"helloWorld.jpg"];
            }
         }

SecondClassViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self cover]; 
}

The cover is a property UIImageView that I created on the SecondClassViewController.

like image 314
user1911 Avatar asked Jan 23 '14 15:01

user1911


2 Answers

When you call prepareForSegue:sender: the outlets from your destinationViewController are not set yet. This means that your photo.cover property is still nil.

What you can do is create a UIImage property on your MovieImageViewController and do something like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"ShowCover"]) {
            MovieImageViewController *photo = [segue destinationViewController];       
            photo.image = [UIImage imageNamed:@"helloWorld.jpg"];
        }
}

and on your -viewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.cover.image = self.image;
}
like image 169
Bruno Koga Avatar answered Nov 06 '22 12:11

Bruno Koga


I'm guessing that "cover" is an image view property of your MovieImageViewController class? If so, don't do that.

You should treat another view controllers views as private.

Instead, set up an image property in your MovieImageViewController class. Set THAT in your prepareForSegue method, and then in your MovieImageViewController's viewWillAppear:animated method, install the image into your "cover" image view. Problem solved.

EDIT: To clarify

Add the following to the header (.h) file for your MovieImageViewController class:

@interface MovieImageViewController

//This property is the line to add
@property (nonatomic, strong) UIImage *coverImage;

//your other stuff goes here.

@end

Then in your .m file:

@implementation MovieImageViewController

- (void) viewWillAppear: animated
{
  [super viewWillAppear: animated];
  //Add the following code to your viewWillAppear:animated method
  if (self.coverImage != nil)
     self.cover.image = self.coverImage;
}

@end
like image 27
Duncan C Avatar answered Nov 06 '22 12:11

Duncan C