1

I have an app that populates list views when a view is created. I would like to be able to navigate with a back button and go back to a previous screen and have the data that was listed still be there. Is this possible?

What seems to be happening with basic creation of a back button, is when I press it, the view starts up like it was brand new, expecting data input to populate the list. Is it possible to have the state of the previous screen saved? rather than it be wiped out and have to be re-populated when using a back button? I would rather not have to ping the server I get the data from every time a user presses the back button. Especially if the data has already been grabbed once already.

In other words, I'd like it to work like a web browser, where the state of the screen is saved and pressing back just loads what was there, rather than pinging the website again for all of its data.

Thanks.

Jesse Durham
  • 285
  • 1
  • 5
  • 18

2 Answers2

2

Definitely possible. The best approach would be to store the data for the list in core data, then whenever your view appears you can check to see if you have data saved, and if not, go to the network and populate the data

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • By core data you mean some kind of global class? I had thought of this solution, but I was more asking if there was some sort of way to save a view's state in memory then recall it.. or something like that. – Jesse Durham Jan 19 '12 at 15:54
  • check out [the core data programming guide](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/coredata/cdprogrammingguide.html). Basically instead of customising how you store the state of each view controller, you store the data which is used to generate the view, and reload from the data store each time the view is created – wattson12 Jan 19 '12 at 16:06
0

You could also use NSUserDefaults to store the data, instead of Core Data. It depends on the data you plan to store.

Update 1 Here is an example to use NSUserDefaults

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* recentlyViewedPhotos = [[defaults objectForKey:@"recentlyViewedPhotos"] mutableCopy];
//Edit your object here
[defaults setObject:recentlyViewedPhotos forKey:@"recentlyViewedPhotos"];
[defaults synchronize];
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
The dude
  • 7,896
  • 1
  • 25
  • 50
  • NSUserDefaults might work if it can handle arrays. (which it looks like it can) I'll have to play around with it some and find out. Core Data is a bit more complicated than what I need. – Jesse Durham Jan 19 '12 at 16:11
  • I've stored an NSArray of NSDictionary's into NSUserDefaults without any kind of problem. – The dude Jan 19 '12 at 16:29
  • I read somewhere the content of the array matters. If I have an array of classes, will that throw it off since its not an array of values NSUserDefaults likes? ie. string, data, int etc. – Jesse Durham Jan 19 '12 at 16:35
  • It won't work by default, you have to implement some extra methods. Here is a question that had the same problem that you: [link](http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults) – The dude Jan 19 '12 at 16:45
  • Ok. I think I may go this route. Thanks. – Jesse Durham Jan 19 '12 at 16:57