Reputation: 779
Okay. It seems pretty easy for some of you guys but I have been facing this issue since very long.
My problem is this:
--> I have 5 tab bar controllers each consisting navigation controllers.
--> Now I have multiple view controllers attached to each tab bar controller like this:
Tab1 -- VCa -> VCb -> VCc
Tab2 -- VCd -> VCe -> VCf
Tab3 -- VCg -> VCh -> VCi
Tab4 -- VCj -> VCk -> VCl
Tab5 -- VCm -> VCn -> VCo
--> When I launch My app and select Tab2, then I navigate to VCd -> VCe -> VCf
--> Now I select Tab5, then I navigate to VCm -> VCn and here I have a button that navigates me back to Tab2.
Tab5, On a button click in VCn
self.tabBarController?.selectedIndex = 1 // Navigate back to Tab2
--> Now here is the issue. I am navigated to Tab2(VCf) successfully. But My problem is that I wish to show default(first/initial) Tab2(VCd).
So how can I go with it?
Any help would be much appreciated. Thanks in anticipation. :)
Upvotes: 1
Views: 1239
Reputation: 11
It looks like you can change the root view controllers for each tab using setViewControllers
.
So something like,
let rootViewControllers = [VCa, VCd, VCg, VCj, VCm]
self.tabBarController.setViewControllers(rootViewControllers, animated: false)
Upvotes: 1
Reputation: 3003
guard let controllers = tabBarController?.viewControllers,
let controller = controllers[1] as? UINavigationController else { return }
controller.popToRootViewController(animated: false)
Upvotes: 2
Reputation: 15218
The easiest way to solve this is to reach into the navigation stack, grab the UINavigationController inside tab 2, and make it pop back to the root view.
self.tabBarController?.selectedIndex = 1
(self.tabBarController?.viewControllers[1] as? UINavigationController)?.popToRootViewController(animated: false)
Note however, that this might fail if you ever change the navigation structure.
Upvotes: 0