Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set UIImageView.image in prepareForSegue

Tags:

objective-c

I want to segue to a photoViewController that has an UIImageView. In flickrTVC, I say:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showPhoto"])
    {
        photoViewController *vc = [segue destinationViewController];
        UIImage *photo = [UIImage imageWithContentsOfFile:@"images/en.jpeg"];
        [vc setDisplayedPhoto:photo];
    }
}

and the setDisplayedPhoto method's implementation is:

-(void)setDisplayedPhoto:(UIImage *)image
{
    [_imageView setImage:image]; //@property (nonatomic, strong) IBOutlet UIImageView *imageView;
}

But when I do segue, the UIImageView is white... Why could this be?

like image 956
Krzysiek Avatar asked Dec 26 '22 21:12

Krzysiek


1 Answers

Your destination view controller hasn't loaded its view hierarchy by the time your source view controller receives the prepareForSegue:sender: message. So your destination VC's _imageView variable is nil.

The recommended approach is to store the image (or, probably even better, the image file name) as a property of the destination VC, and then set the image view's image in the destination VC's viewDidLoad or viewWillAppear: method.

like image 84
rob mayoff Avatar answered Jan 12 '23 10:01

rob mayoff