Henry F
Henry F

Reputation: 4980

Adding Image To UIImageView In Separate Class

I have two view controllers: Products View Controller and Detail View Controller. When the user taps a cell on a UITableView, it will transition to the Detail View Controller. The Detail View Controller has a UIImageView in it names imageEq. I'm trying to set the image in the Products View Controller. This is how I'm doing it:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushDetailView"])
    {
        DetailViewController *dvc;
        UIImage *pic = [UIImage imageNamed:@"HorseImage.png"];
        [dvc.imageEq setImage:pic];
        NSLog(@"Pic should be set");
}
........ (non relevant code)
}

I connected the outlets, copied the image into the project, and made sure all my spelling is correct. When I run this, the image view remains blank and has never loaded the image. I can't figure out why not, this all looks perfect to me. Does the above code look flawed in any way?

Upvotes: 0

Views: 71

Answers (2)

Jim Tierney
Jim Tierney

Reputation: 4105

If you replace your DetailViewController pointer to this code below, it should work for you.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushDetailView"])
    {
        UIImage *pic = [UIImage imageNamed:@"HorseImage.png"];

        [[segue destinationViewController] setDetailImage:pic];

        NSLog(@"Pic should be set");
}

Hope this helps, Thanks Jim.

EDIT

Add the following property in your DetailViewController's header file

@property (strong, nonatomic) UIImage *detailImage;

And in the viewDidLoad of your DetailViewController's .m file, add the following

self.imageEq.image = _detailImage;

I hope this updated answer help.

Upvotes: 1

Avt
Avt

Reputation: 17043

Also change

[[segue destinationViewController] setImageEq:pic];

to

[[segue destinationViewController] setImageEq:[[UIImageView alloc] initWithImage:pic]];

Upvotes: 1

Related Questions