iosios6segueuicontainerviewparentviewcontroller

Container View Controller cannot handle Unwind Segue Action


I am facing a problem while trying to unwind using a custom segue from a view controller that was added as a child to another view controller.

Here is MyCustomSegue.m:

- (void)perform
{
    if (_isPresenting)
    {
        //Present
        FirstVC *fromVC = self.sourceViewController;
        SecondVC *toVC = self.destinationViewController;

        toVC.view.alpha = 0;

        [fromVC addChildViewController:toVC];
        [fromVC.view addSubview:toVC.view];

        [UIView animateWithDuration:1.0 animations:^{

            toVC.view.alpha = 1;
            //fromVC.view.alpha = 0;

        } completion:^(BOOL finished){

            [toVC didMoveToParentViewController:fromVC];

        }];

    }
    else
    {
        //Dismiss
    }
}

And here is my FirstVC.m:

- (void)prepareForSegue:(MyCustomSegue *)segue sender:(id)sender
{
    segue.isPresenting = YES;
}

- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
{
    return self;
}

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    return [[MyCustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}

- (IBAction)unwindToFirstVC:(UIStoryboardSegue *)segue
{
    NSLog(@"I am here");
}

All the necessary connections are done in the storyboard as well.

My problem is that -segueForUnwindingToViewController: is never called. As soon as -viewControllerForUnwindSegueAction:fromViewController:withSender: is returned, my program crashes with the following exception:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <FirstViewController: 0x8e8d560>'

As of my understanding, the reason for the crash, is that I want my container view controller to be the one to handle the unwind segue action, which is not possible (because container view controller asks only its children to handle the unwind segue.

Is it correct? What can I do to solve my problem?

Thanks!


Solution

  • I don't think you can use an unwind segue that way (at least I couldn't find a way). Instead, you can create another "normal" segue from the child controller back to the parent, and set its type to custom, with the same class as you used to go from the first controller to the second. In prepareForSegue in the second controller, set isPresenting to NO, and have this code in your custom segue,

    - (void)perform {
        if (_isPresenting) {
            NSLog(@"Presenting");
            ViewController *fromVC = self.sourceViewController;
            SecondVC *toVC = self.destinationViewController;
            toVC.view.alpha = 0;
            [fromVC addChildViewController:toVC];
            [fromVC.view addSubview:toVC.view];
    
            [UIView animateWithDuration:1.0 animations:^{
    
                toVC.view.alpha = 1;
    
            } completion:^(BOOL finished){
                [toVC didMoveToParentViewController:fromVC];
            }];
    
        }else{
            NSLog(@"dismissing");
            SecondVC *fromVC = self.sourceViewController;
            [fromVC willMoveToParentViewController:nil];
            [UIView animateWithDuration:1.0 animations:^{
                fromVC.view.alpha = 0;
            } completion:^(BOOL finished) {
                [fromVC removeFromParentViewController];
            }];
        }
    }