How to Unwind Through Multiple Views Without Displaying Intermediate Views

How to unwind through multiple views without displaying intermediate views

Josh's answer led me to a solution. Here's how to accomplish this:

Create a root UINavigationController, and assign it to a class that extends UINavigationController and overrides the segueForUnwindingToViewController:fromViewController:identifier method. This could be filtered by the identifier if desired.

CustomNavigationController:

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

Create a custom push segue, that behaves like a modal segue, but utilizes our navigation controller. Use this for all "push" segues.

CustomPushSegue:

-(void) perform{
[[self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:NO];
}

Create a custom unwind segue, that uses the navigation controller to pop to the destination. This is called by our navigation controller in the segueForUnwindingToViewController:fromViewController:identifier method.

CustomUnwindSegue:

- (void)perform {
[[self.destinationViewController navigationController] popToViewController:self.destinationViewController animated:NO];
}

By utilizing a combination of these patterns, the second view controller never appears during the unwind process.

New log output:

  • one did appear
  • #### segue to two
  • two did appear
  • #### segue to three
  • three did appear
  • #### unwind to one
  • one did appear

I hope this helps someone else with the same issue.

Unwind then segue without showing intermediate view controller

Try this Code in swift 3 :-

let oldVCs = self.navigationController?.viewControllers
var newVCs = [UIViewController]()

// Add your root VC in new array of VCs
newVCs.append(oldVCs![0])
// Add your new VC just after your root VC
newVCs.append("Your New VC")

self.navigationController?.setViewControllers(newVCs, animated: true)

Unwind To Specific ViewController Without NavigationController

In each VC you want to unwind to you need to create an @IBAction func with one argument (_ segue UIStoryboardSegue) then in Your storyboard you go to the vc you want to unwind from and on the top control click drag between th VC icon and the exitnicon. You need to choose the unwind function and then a new segue appears in your VC tree on the left hand side. Clicking on the segue gives you an option as with regular soryboard segues togive it an ID. Once named you can programatically perform segue with identifier. You can add as many of these segues as you like.



Related Topics



Leave a reply



Submit