In my storyboard the App workflow design is like this
-> NavigationController -> ViewControllerX -> TabBarController -> NavigationController -> ViewControllerA
-> NavigationController -> ViewControllerB
-> NavigationController -> ViewControllerC
Regarding the encapsulation design pattern I wonder which of the following approaches would be best to share data on segueing from ViewControllerX to the TabBarController containing ViewControllerA/B/C:
-
Subclassing the TabBarController with a custom property. Then on segueing the data is passed to the TabBarController. Each of the
ViewControllerA/B/C“pulls” the data inviewDidLoadwithCustomVar *myCustomVariable = self.tabBarController.myCustomVariable. -
Create a singleton.
I’d prefer approach 1 as there are some more ViewControllers in the project and the sharing is only between ViewControllerX and ViewControllerA/B/C. Are there any issues with that?
I don’t see any issue at all. That’s the way I go every time I face a similar situation. With a “simple” situation like this one, no need to look for something more complicated than that.
I don’t really have anything to support my answer, except my own experience. I see nothing bad in the approach 1.
About the solution 2, I usually use the AppDelegate “as a singleton” when I have a data needed throughout the application. I’ve heard good and bad things about that, but I have never faced anything bad, and I’ve been using it quite a lot. But given your situation, I’d rather go with solution 1. I’d say solution 2 is for data used in the whole application workflow.
EDIT :
Here’s how to use your AppDelegate as a singleton.