I've got a TabBarViewController controlling 5 UIViewControllers. I'm trying to pass data (a user object in particular) from one UIViewController. What I'm currently doing is to set the user property in the TabBar Controller from within one of the UIViewControllers (HomeScreen) like so:
class HomeViewController: UIViewController { let apiService = APIService() var user: User? var parentController: TabBarController? override func viewDidLoad() { super.viewDidLoad() let token = UserDefaults.standard.string(forKey: "authtoken")! self.apiService.getUserFromAuthtoken(token: token, completion: {result in switch result { case .success(let user): self.user = user self.parentController = self.tabBarController as? TabBarController self.parentController?.user = user print("EMAIL-HOME-SCREEN: ", user.email) case .failure(let error): print("An error occured \(error.localizedDescription)") } }) } } Then, after navigation, I'm trying to access the TabBarView's user property from another UIViewController (CoopOverview) like so:
class CoopOverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var CampaignBrandSliderBackground: UIView! @IBOutlet weak var CampaignCollectionView: UICollectionView! @IBOutlet weak var CampaignBrandSlider: UISegmentedControl! var user: User? var parentController: TabBarController? @IBOutlet weak var foo: UIView! let campaignImages: [UIImage] = [UIImage(named: "icon-black")!,UIImage(named: "icon-white")!,UIImage(named: "icon-black")!,UIImage(named: "icon-white")!] override func viewDidLoad() { super.viewDidLoad() self.parentController = self.tabBarController as? TabBarController self.user = self.parentController?.user print("EMAIL-COOP-SCREEN: ", user!.email) ... } Is this a good approach or is there a more elegant way to solve this?