I am reading data from the JSON object and pass the image property to other viewcontroller. Before I do it, I wanted to test the following code with hardcoded UiImage and it works perfectly fine.

   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

        GrillMenuSingleFoodViewController *gVC = [segue destinationViewController];

        if ([segue.identifier isEqualToString:@"isMoreDetail"]) {

            [gVC setImage:[UIImage imageNamed:@"Ali_nazik.jpg"]];

        }
}

But the following code is not working. Even though I am getting imagedata, once I pass to other viewcontroller it shows nil.

  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

        GrillMenuSingleFoodViewController *gVC = [segue destinationViewController];

        if ([segue.identifier isEqualToString:@"isMoreDetail"]) {

            NSString *imageURLString=[menuArray[indexPath.row]valueForKey:@"picture"];;
            NSURL *url = [NSURL URLWithString:imageURLString];
            NSData *data = [[NSData alloc] initWithContentsOfURL:url];
            UIImage *tmpImage = [[UIImage alloc] initWithData:data];
            [gVC setImage:[UIImage imageWithData:data]];
        }
}
有帮助吗?

解决方案

Change your "@property" declaration to strong.

I.E.

@property (strong) UIImage *image;

You need to have a retained image for it to be picked up in your new view controller.

Otherwise it will be disposed by the parent view controller when the "prepareForSegue" method finishes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top