简体   繁体   中英

Pop UIViewController using Custom Segue?

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?

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

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];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM