Hello,
Those forums are dedicated to the Swift language, not to Apple platforms, so your question is a little bit off-topic here. You are encouraged to ask this kind of question on Apple Developper forums, or StackOverflow.
Now, you are here, and it's possible to give a suggestion.
Ideally, both view controllers should not know about each other, so that they can live in isolation, and be tested. This means that the communication between the two has to be delegated to a third object.
Delegation is a standard iOS technique, that you already know about, according to your screenshot:
protocol LeftControllerDelegate: AnyObject {
func leftControllerDidHitButton(_ controller: LeftController)
}
class LeftController: UIViewController {
weak var delegate: LeftControllerDelegate?
@IBAction func didHitButton(_ sender: UIButton) {
delegate?.leftControllerDidHitButton(self)
}
}
Which object should be the delegate? In your case, it's easy: it is the object that creates both view controllers and puts them in the UITabBarController. It has all the required information.
If your app creates the UITabBarController programmatically, from some class, then this class is the perfect delegate:
func makeTabBarController() -> UITabBarController {
let left = /* LeftViewController */
self.right = /* RightViewController */
left.delegate = self // <-- there
let tabBarController = UITabBarController()
tabBarController.viewControllers = [left, right]
return tabBarController
}
func leftControllerDidHitButton(_ controller: LeftController) {
self.right.functionFromMain("Hello")
}
If your app lets the storyboard magic create the UITabBarController, then I'd suggest creating a subclass of UITabBarController, say MainTabBarController, update the storyboard and change the class of the tab bar controller to MainTabBarController, and make this tab bar controller the delegate of its child:
class MainTabBarController: UITabBarController: LeftControllerDelegate {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let left = viewControllers.first as? LeftViewController {
left.delegate = self // <--
}
}
func leftControllerDidHitButton(_ controller: LeftController) {
if let right = viewControllers.last as? RightViewController {
right.functionFromMain("Hello again")
}
}
}