0

I am following this answer to "release" my previous view controller in a UINavigationController.

It works fine however the popping part is the code I am having difficult in getting to work. Basically my app works like this. It starts on a main menu (View 1) then it pushes to View 2 and I use the custom push segue to get to View 3. Now I want to use a different custom segue for popping now to go from View 3 to View 2. However, by using the code below, it pops to View 1 very quickly and then eventually pushes to View 2. It looks like the view controller transition is unnatural and I am just looking to achieve the usual pop transition just instead by using a custom segue to "release" the source view controller.

This is my code I am using now to no avail:

- (void)perform {
    // Grab Variables for readability
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;

    // Get a changeable copy of the stack
    NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers];
    // Replace the source controller with the destination controller, wherever the source may be
    [controllerStack addObject:destinationController];

    // Assign the updated stack with animation
    [navigationController setViewControllers:controllerStack animated:YES];
}

Is there something I am doing wrong here?

Community
  • 1
  • 1
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • Have you try [navigationController pushViewController: destinationController animated:YES]; – Nirav D Jun 14 '16 at 05:41
  • @NiravDoctorwala Yes, however that doesn't fix the issue. And another issue is that I am specifically looking for a "pop" animation, not a "push." – SimplyKiwi Jun 14 '16 at 05:46
  • http://stackoverflow.com/questions/10281545/removing-viewcontrollers-from-navigation-stack – Rushi trivedi Jun 14 '16 at 06:50

3 Answers3

1

What you want is an "unwind" segue. More about those here: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

Dancreek
  • 9,524
  • 1
  • 31
  • 34
0

If you just want to pop View 3 to go back to View 2, can't you just do something like this?

- (void)perform {
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;
    [navigationController popViewControllerAnimated:YES];
}
tahavath
  • 234
  • 2
  • 9
  • This is not what I need though because popping in my case goes from View 3 -> View 1. I am trying to pop to view 2 – SimplyKiwi Jun 14 '16 at 16:10
0

I am not sure if this answer is localized to me but after logging my navigation stack hierarchy and playing around with the array, I did this and it works well for me.

- (void)perform {
    // Grab Variables for readability
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;

    // Get a changeable copy of the stack
    NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers];
    [controllerStack replaceObjectAtIndex:1 withObject:destinationController];
    [controllerStack addObject:sourceViewController];

    [navigationController setViewControllers:controllerStack animated:NO];
    [navigationController popViewControllerAnimated:YES];
}

A more widespread answer would probably be to find the index of the source view controller object in the array and add your destination view controller to the index before it, shifting everything from that prior index on forward one place, that way you don't mess up any of your other view controllers. As I said, the index 1 is what worked for me in this case particularly.

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191