To be more specific, I have an app that has multiple tableViews that drill down data to display an image in a detail view. When the detail view is displayed, the app adds this views image (its name) to an array of recently viewed images. The recently viewed images are displayed in a tableview in the first screen of the app.
The problem lies in the fact that the detail view is created by a tableView at the end of the line of a list of tableViews. The detail view is also created by the first tableView when a recent item is tapped on.
I tried to setup the first tableView as a delegate of the detail view, to receive notification that a new image has been displayed in the detail view. I realized it never calls the delegate methods when a new item is entered, since the detail view is being created somewhere else.
I think I need to rethink my app design and who should hold the data for the recently viewed images. I thought the first view should hold that data since it is the one displaying it, but I am at a bit of a loss on how to do this in a way that takes good code design into account. I can make it work with NSUserDefaults, but am not sure if this is really what it should be used for or if this is the best practice.
I appreciate any and all input.
Jamie
I would see a few solutions here:
1) go ahead with NSUserDefaults, LRU (last recently used I guess) lists are often being save to user preferences, regardless if it is a good or bad design. If it doesn’t take to much space and works for you just go ahead.
2) make a common context for LRU, possibly using a singleton, that would be accessible from all over the application, put your images there and get them back any time you need them. Actually you could make its interface using class methods and not bother making it real singleton. Class methods represent a nice static context that is easily accessible. In ObjC they even can be overridden! Which is kind of cool compared to Java.
3) use a factory pattern to create views: there is one factory class that know the delegate, creating new views would be again implemented using class methods: each create method would the set the one known delegate to each view regardless of the point of creation.
4) now this one is really overkill but very very “un-coupled”: send notifications with recently used images. Those that are interested in knowing them would register themselves are observers and, well, get the images…