0

I have a custom segue:

-(void)perform {

    __block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    __block UIViewController *destinationController = (UIViewController*)[self destinationViewController];

    float duration = 0.5;

    NSLog(@"FlipSegue.m - source:%@ dest:%@", sourceViewController, destinationController);

     if (self.isUnwinding) {
         NSLog(@"FlipSegue.m - isUndwinding - Flipping View 2 to View 1");
         //[sourceViewController.navigationController dismissViewControllerAnimated:NO completion:nil];
         [UIView transitionWithView:sourceViewController.navigationController.view duration:duration
                            options:UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             [sourceViewController.navigationController dismissViewControllerAnimated:NO completion:nil];
                         }
                         completion:NULL];
     } else {
         NSLog(@"FlipSegue.m - Going Forward - Flipping View 1 to View 2");
         //[sourceViewController.navigationController pushViewController:destinationController animated:NO];
         [UIView transitionWithView:sourceViewController.navigationController.view duration:duration
                            options:UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             [sourceViewController.navigationController pushViewController:destinationController animated:NO];
                         }
                         completion:NULL];
     }

}

I have setup the segue and unwind segue using a storyboard.

I have subclassed UINavigationController and am getting the custom unwind segue using - (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)

When I invoke the segue from View 1 to View 2, it works as expected.

When I invoke the unwind segue, View 2 flips, but displays View 2 again. The isUnwinding portion of code fires but View 1 never reappears.

Any tips about how to fix this issue would be greatly appreciated.

Chris
  • 5,485
  • 15
  • 68
  • 130

2 Answers2

1

Did you set up the unwind segue though in storyboard?

Add - (IBAction)unwind:(UIStoryboardSegue *)segue; in ViewController 1. Go to view2 in storyboard. Ctrl drag from the view to exit. Select the unwind method. Go to the outline in storyboard and click on the unwind segue you just made. Change its class to the custom segue.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • Doesn't the `- (IBAction)unwind:(UIStoryboardSegue *)segue` go in ViewController 1? – Chris Jan 07 '15 at 17:54
  • 1
    I noticed that from View1 to View2, you are using pushViewController, but from View2 to View1 you are using dismissViewController. The opposite of pushViewController is popViewController, while the opposite of dismissViewController is presentViewCOntroller. Perhaps you are mixing them? Dismiss and Present are for modal segues. – Josh Gafni Jan 07 '15 at 18:38
  • Getting closer... Changing `dismissViewController` to `popToRootViewController` almost gets me there. The screen flips, continues to show `View 2`, but then pops to `View 1`. Just seems like I need to get it to pop before I flip it. If I put `popToRootViewController` before `transitionWithView:duration:...`, it pops correctly to `View 1` but does not show the animation. – Chris Jan 07 '15 at 19:20
  • I might have found an answer here http://stackoverflow.com/questions/3602434/transition-behavior-using-transitionfromview-and-transitionwithview – Chris Jan 07 '15 at 19:26
0

As suggested by @JoshGafni, I was able to fix this problem by using popToRootViewController and fussing with the code a bit. Push/Pop is working but I could not get Present/Dismiss to work without adding additional problems to the existing code.

Here's the end result that worked:

if (self.isUnwinding) {
         NSLog(@"FlipSegue.m - isUndwinding - Flipping View 2 to View 1");

         [UIView transitionWithView:sourceViewController.navigationController.view
                           duration:duration
                            options:UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             [sourceViewController.navigationController popToRootViewControllerAnimated:NO];
                         }
                         completion:nil];
     } else {
         NSLog(@"FlipSegue.m - Going Forward - Flipping View 1 to View 2");

         [UIView transitionWithView:sourceViewController.navigationController.view duration:duration
                            options:UIViewAnimationOptionTransitionFlipFromLeft
                         animations:^{
                             [sourceViewController.navigationController pushViewController:destinationController animated:NO];
                         }
                         completion:nil];
     }
Chris
  • 5,485
  • 15
  • 68
  • 130