You can use delegation. I will explain with a bit simpler example with two tab bars but you can transform it to your needs.
First of all, we have to create a protocol and a function that we want to be executed in our desired view controller.
protocol DataDelegate: class {
func didReceiveData(data: String)
}
Let's say we have FirstViewController which is for the first tabBar and SecondViewController for the second tabBar. In the SecondViewController we have to declare a weak variable for our delegate.
// In SecondViewController
weak var dataDelegate: DataDelegate?
In the FirstViewController we have to conform to the UITabBarControllerDelegate(do not forget to set the delegate to self in viewDidLoad self.tabBarController?.delegate = self
) and use the delegate method tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController)
// MARK: - TabBarControllerDelegate
extension FirstViewController: UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if let secondViewController = viewController as? SecondViewController {
secondViewController.dataDelegate = self
}
}
}
As we can see viewController is the ViewController that we have tapped on(in our case SecondViewController). We have to cast it to SecondViewController so we have access to it's properties and set dataDelegate to self.
Now we are obligated to conform to the DataDelegate protocol. We have only one function that we have to implement - didReceiveData().
// MARK: - DataDelegateProtocol
extension FirstViewController: DataDelegate {
func didReceiveData(data: String) {
print(data)
}
}
Finally, in our SecondViewController we must tell where we want that function to be executed. I will do it viewDidAppear but you can wherever you need to.
// In SecondViewController
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.dataDelegate?.didReceiveData("data")
}
This guy explains delegation well link. Hope you got it.