If you configured correctly such storyboard, you should get the reference to the UITabBarController
through prepare(segue)
, doing so:
private weak var tabBarViewController:UITabBarController?
// be sure to add prepare inside your segue manager (the container in this case)
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.tabBarViewController = segue.destination as? UITabBarController
}
besides you may want to declare an enum to have a clean access to the embedded view controllers:
enum TabType:Int {
case blue
case green
}
thus you might define a function for injecting the selected indexPath, doing something like:
func inject(indexPath:IndexPath, into tab:TabType) {
if let greenVc = tabBarViewController?.viewControllers?[tab.rawValue] as? GreenViewController {
greenVc.selectedIndexPath = indexPath
tabBarViewController?.selectedIndex = tab.rawValue // this will auto select the TabType viewcontroller
}
}
finally whenever you have to change the current selection, you may call inject with the new indexPath:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.inject(indexPath: indexPath, into: .green)
}
Note
Call instantiateViewController
means making a new "fake" view controller, which is a wrong approach, since you already have it embedded in your UITabBarController.viewControllers