Reputation: 51
I’m using a TabBarController with 2 tab bar items and a global variable. My idea is that tab 1 alters the value of the global variable, and tab 2 shows it updated.
My problem is that tab 2 is never reloaded/updated with the new value of the global variable after being modified.
I have read some questions exactly with the same problem and they point to didSelectViewController but this method never seems to be called in my UITabBarController (yes, it is assigned in the Storyboard and it extends UITabBarControllerDelegate; in fact, didSelectItem does work).
Any kind help with this, please?
Many thanks in advance!!
Upvotes: 1
Views: 3379
Reputation: 4065
You should be focused on the UIViewControllers for each tab, not the tab controller itself.
In the UIViewController sub-class for the first tab, you can modify the global variable.
In the UIViewController sub-class for the second tab, you access this global variable. You can override the method viewWillAppear(animated: Bool)
to know when to update the variable.
You could also use protocols and delegation to update the value only when it is changed.
Upvotes: 2