Your question title asks about navigation controller, but the rest of your question and the image is about tab bar controllers, so I'll assume that is what you are asking about.
There are two different cases here:
- When the various contained view controllers are first created; This is handled by the relationship segues, but these are not segues that result in
prepare(for:sender:) being called. These "segues" just represent the relationships in the storyboard that enable the UITabBarController to load the embedded view controllers.
- When the user switches between the different tabs - This seems to be way you are asking about. This is not handled using segues, so putting code in
prepare(for:sender:) won't help you.
What you can do is create a subclass of UITabViewController and use that in place of the standard UITabViewController in your storyboard.
Now, you can override viewDidLoad in your subclass and access the embedded view controllers via self.viewControllers - This gives you initial access to those view controllers so that you can customise their initial state.
What about handling tab switches?
UITabViewController conforms to UITabBarDelegate. This means your subclass can implement [tabBar(_:didSelect)](https://developer.apple.com/documentation/uikit/uitabbardelegate/1623463-tabbar). This function is called each time the user selects a tab. In this function you can use the selectedViewController property to get the newly selected view controller, cast it to the appropriate type and set its properties as required.
class MyTabBarController: UITabBarController {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let vc = self.selectedViewController as? Tab0ViewController {
vc.someProperty=someValue
} else if let vc = self.selectedViewController as? Tab1ViewController {
vc.someProperty=someValue
}
}
}
However, this may not be necessary if you set up your view controllers to use a shared data model.
Your view controllers can observe the model and whenever your model is updated, those changes can be communicated to the various view controllers through some other method; You could use Combine or Notifications, for example.
Then, when the user switches tabs the view controller is already showing the right information.
It depends on exactly what you are trying to achieve.