user1911
user1911

Reputation: 710

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.

Upvotes: 5

Views: 1505

Answers (2)

Duncan C
Duncan C

Reputation: 131426

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

Upvotes: 2

Bruno Koga
Bruno Koga

Reputation: 3874

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;
}

Upvotes: 6

Related Questions