By default, the UITabBarController expects to be given an array of view controllers to display as tabs. However, there may be situations where you want to programmatically set up the tab bar without providing this array. I'm wondering how i could do this.
Ultimately, the UITabBarController
needs to have an array of view controllers, but perhaps your code is split up in such a way that no single function or object knows all the tabs.
In that case, start with an empty array. Then, each part of your code that knows a single tab can use the viewControllers
member and the setViewControllers
method to add their tab to the array. The UITabBarController
is smart enough to recognize if some of the VCs are from the original set and keep any state it has associated with it (like if it was selected).
See documentation here: https://developer.apple.com/documentation/uikit/uitabbarcontroller/1621177-setviewcontrollers