I would like some expert MVC design feedback please:
I have a UITabBarController with 2 tabs, each of the tabs leads to a Nav controller with a stack of VC.
The last view Controller on the 1st tab path will show an image. I would like that image to be stored in a table and viewed anytime the second tab is selected.
How do I send this image from the 1st tab-> NaVController->last VC (image VC) to 2nd tab -> NavController->table VC?
I have a couple of options:
1- Create a class method in the tableVC and have the imageVC call that class method and pass that image to be saved directly into user defaults. This seems to go against MVC
2- Create a protocol in imageVC with a method and delegate property and have the table VC adopt that method to save the image in an array. The problem here is that the only place to set the delegate is in ViewDidLoad:
[[[[self.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:1] setDelegate:self];
The problem here is that if the user select the second tab from the start, the app will crash because obviously the viewControllers have not been loaded on the nav stack for the first tab. In the same token, if the user sees 1 image first and then selects the second tab, it will execute and set the delegate but without saving that first selected image.
There must be an easier way…..
Thanks in advance
KB
I think that you should separate your data model from your view controllers a little more. Following the MVC approach, a model should not know about how its represented. So, making a change to the model in
ImageVCshould not involve direct calls to any other VC.I’d create a separate entity, e.g.
SelectedImages, and add images fromImageVCto it, and haveTableVCread from it when it needs to display itself.SelectedImagescould be a property in the app delegate, a Core Data entity, a singleton class, or something like that.