One way is to use the navigational heiarchy to access the view controller.
From the viewController that you want to pass data it's tabBarController
property should be the same instance of the tab bar that VC2
is a part of
func setStringForVC2(stringToSet:String) {
guard let myTabBarController = tabBarController, let tabBarViewControllers = myTabBarController.viewControllers else {
return
}
//need to access index 1 since vc's are stored in tabbarcontroller starting with 0, so 1 to access 2nd vc.
if (tabBarViewControllers.indices.contains(1)) {
if let vc2 = tabBarViewControllers[1] as? VC2Class {
vc2.stringIWantToSet = stringToSet
}
}
}