Question

I'm pretty new with the iOS stuff, and thought I understood passing data to a view controller. Apparently not. I am trying to pass image data from a parent ViewController to a child ViewController that is presented modally. I think the problem has something to with the way objects are passed to a modally presented view controller instead of one pushed onto the navigation stack. Bellow is the code from the parent and the child related to the problems I am having.

From the Parent.m:

- (IBAction)sort:(id)sender {
    SortSelectViewController *ssvc = [self.storyboard instantiateViewControllerWithIdentifier:@"sort"];
    ssvc.backgroundImage = [self screenshot];
    [self performSegueWithIdentifier:@"sort" sender:self];
    //The segue is linked from my parent view controller to my child with the identifier "sort"

}

- (UIImage *) screenshot {
    CGRect rect = CGRectMake(0, 20, 640, 1116);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    return image;
}

This is in my Child.h

@property (nonatomic, strong) UIImage *backgroundImage;

This is my Child.m:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%@", self.backgroundImage);
    //This log will return nil when the program is run - clear sign data is not being correctly passed
    self.backgroundImage = [self.backgroundImage applyDarkEffect];
    self.imageView.image = self.backgroundImage;
}

I have imported my child view controller into my parent class

Was it helpful?

Solution 2

try Like this By using Story Bord Must use combination of two Methods performSegueWithIdentifier: *prepareForSegue:*

    - (IBAction)sort:(id)sender {

        [self performSegueWithIdentifier:@"sort" sender:self];
        //The segue is linked from my parent view controller to my child with the identifier "sort"

    }



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

        if ([[segue identifier] isEqualToString:@"sort"])
        {
           SortSelectViewController *ssvc = [segue destinationViewController];
        ssvc.backgroundImage = [self screenshot];
        }
     }

- (UIImage *) screenshot {
       //return normal image 
    return [UIImage imagenamed:"imagename.png"];
}

OTHER TIPS

Your code (original question) is conflating direct instantiation of view controller and storyboard segues. When you use a storyboard segue, the VC is instantiated automatically for you. You access it as the "destination view controller" in the segue in "prepareForSegue:" method.

What's happening is that you instantiate a view controller, set the image, then… it gets thrown away and ignored (the entire vc).

Next, you initiate a segue, which iOS handles by instantiating another, different view controller. It's this view controller that iOS is presenting.

So: if you're going to use a Storyboard segue, iOS instantiates the VC for you. Don't instantiate one yourself, because there's no way to link the one you instantiate into the iOS segue handling. The one you instantiate is an irrelevant extra that gets ignored.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top