I’m trying to create an user interface like the one is presented in App Store application (iPad device). This is a sort of scrollable grid (horizontal scroll) where each element consists in an overview of an application that can be selled in the app store (each element consist of an image, a title, and so on). My goal is the same. I’m interested in developing a UIScrollView where elements are arranged and displayed. Each UI element consists in a image and a description.
Moving from the previous considerations, I developed an ItemViewController.h/.m and the relative .xib (ItemView.xib). Each element can respond to actions like the tap. The ItemViewController.m manages this tap since it’s its File’s Owner. An element is created with initWithNibNamad method in the class MainViewController.h/.m. This class contains the UIScrollView. Then, the view associated with the item is added to the UIScrollView.
Since it’s possible to have more than one item, could you give me some suggestions to manage the ItemViewController items in MainViewController? Do I have to store each reference to ItemViewController in a dictionary (for example)? In a first moment I also considered to set MainViewController as ItemView.xib File’s Owner, but I think the problem for managing items could remain.
Thank you in advance. I hope it’s all clear.
It sounds to me like you have this structured backwards. You shouldn’t be storing items presented in by the interface in your controller. You should have a model that contains your items that your controller uses to construct the views of your interface.
So you have a model containing a list of products and their associated metadata. Your controller queries that list, constructs a view for each item in it, and adds those views to the app’s view hierarchy (maybe in a scroll view, like you suggest). Any interaction with those views calls back to the controller which then makes changes to the model. Any changes to the model should post a notification that the controller listens for so that it knows to update (or just reload) its presented views appropriately.
Note that in this arrangement, one item in your model can be represented in the interface multiple times. Say you’re presenting a list for items tagged as “Giant” and a list for items tagged as “Green”. If you had a “Jolly Green Giant” product, it would be represented in both lists. But because the same item in your model is the source for both, any changes to one is automatically reflected by the other. This is generally the desired behavior (if you deleted “Jolly Green Giant” from the “Green” list, you’d also want it to be deleted from the “Giant” list).
For more information, read up on the Model View Controller (MVC) pattern.