0

I have a tab based app. And I wonder if its possible to unwind the user from anywhere to the root of the current tab.

My app is built like this: TabController -> (tabs) -> navigationController -> vc etc..

So lets say that I press on tab 4 in my app. Now I dig through two viewcontrollers and end up in a modal presented VC. Now I want to go back straight to the root of the tab:

tab 4 -> navigationController -> rootVC -> pushVC -> pushVC -> modalVc

So I eg from modalVc to rootVC

I know that I can do something like this, BUT I want to go to the rootVC from any VC within tab4, and using that technique would force me to create a unwind segue on all of my child VCs in tab 4

I am using notifications and when a user press on the notification I want to take the user to the rootVC from anywhere.

And using something like:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "accountVc") as? AccountViewController
                      navInTab.pushViewController(destinationViewController!, animated: false)

Does not work for eg: vc -> vc -> modalVc

since it will mess up the navigationstack

Community
  • 1
  • 1
user2636197
  • 3,982
  • 9
  • 48
  • 69

1 Answers1

0

You can pop to the root of a navigation stack, or pop to the nth element in the stack, or replace the stack of view controllers with a different stack.

You can use popToRootViewController on the navigationController to go back to the very root of a navigation stack. Or suppose you want to go back to the first VC in the stack for example, you would do something like

let controllerStack = self.navigationController?.viewControllers
_ = self.navigationController?.popToViewController((controllerStack?[0])!, animated: true)

Or you can replace the navigation stack of view controllers with a new stack of controllers i.e.

NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// Replace the source controller with the destination controller, wherever the source may be
[controllerStack replaceObjectAtIndex:[controllerStack indexOfObject:sourceViewController] withObject:destinationController];

// Assign the updated stack with animation
[navigationController setViewControllers:controllerStack animated:YES];
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378