unwind with custom segue on UINavigationController pop via “back” button

前端 未结 3 902
青春惊慌失措
青春惊慌失措 2021-02-04 18:32

Simple storyboard setup: UIViewController with UINavigationController. On a table cell click a custom segue pushes a new UIViewController onto the navigation stack. All good. Bu

相关标签:
3条回答
  • 2021-02-04 19:25

    You can accomplish this by creating a custom transition instead of using a custom segue. It took me a while to understand, but it is basically very simple. Just make sure that your first view controller adopts the UINavigationControllerDelegate protocol. Then, still in the first view controller, implement the following method:

    - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC
    {
    return X // where X is an animation Controller object, an instance of the animator class.  
    }
    

    Still in the first view controller, for example in ViewDidLoad, tell the navigationcontroller that the first view controller will act as its delegate:

    self.navigationController.delegate = self;
    

    Last but not least, create an animator class that adopts the UIViewControllerAnimatedTransitioning protocol. Implement the following methods:

    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    

    and

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
    

    The first method holds the transition code. This transition code will automatically be used when tapped on the back button.

    Here is a very simple example project.

    0 讨论(0)
  • 2021-02-04 19:25

    Have you tried using something like:

    - (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
    {
        return [MyCustomSegueInitMethod viewControllerA:vc1 viewControllerB: vcB];
    }
    

    I've never made a custom segue, but assuming they work like all others do when overriding them, as long as your custom segue class is subclassing the UIStoryBoardSegue, it should work...

    Then make sure you've linked your back button to the "exit" icon in the bar underneath the view controller.

    0 讨论(0)
  • 2021-02-04 19:29

    Unwind segues are useful to “roll back” segues, including custom segues. The problem in your usecase does not lie with segues per se, but with the fact that you want to trigger the unwind segue from the back button.

    This said, you could try to work around this by not using unwinding, but trigger a custom segue when the standard-back-button is tapped:

    // MyDetailViewController.m:
    - (void)viewWillDisappear:(BOOL)animated
    {
        if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
            // trigger custom segue (but note that the sourceViewController
            // has already been removed from the nav controller)
        }
        [super viewWillDisappear:animated];
    }
    

    Obviously this no longer has anything to do with unwinding - it’s just performing a custom segue that you implement to “roll back” the segue you used to “push” the detail view controller in the first place.

    Let me know how this goes.

    0 讨论(0)
提交回复
热议问题