views:

638

answers:

5

I would like to learn the best practices in reloading the application state, such that when my app is started, it should automatically load the "correct" view/subview when it is opened again.

In my particular case, my app has a bunch of view controllers, each taking care of a UITableView. I would like for my app to "jump" to the correct node within my table view hierarchy when it is opened again.

+1  A: 

You'll need to associate an identifier with each view controller, and save that value to the user defaults when the current view changes. The next time you launch, read that value and you'll know which view controller you need to load initially.

Marc Charbonneau
A: 

Maybe you could read a bit on the memento pattern :)

+4  A: 

Building on what Marc said, assuming you've got a base view controller, and then one or more levels of 'drill-down', load all your view controllers up to the 'current' one using [navigationController pushViewController: viewController animated: NO]. Then, when the user hits the Back button, they'll be presented with the pre-loaded previous view controller. A good example of this is the "Contacts" app, which preloads the Groups view controller, then pushes a view controller for the current group (usually "All Contacts") on top of that.

Ben Gottlieb
+3  A: 

Check Apple's DrillDownSave sample app:

"Demonstrates how to restore the user's current location in a drill-down list style user interface and restore that location when the app is relaunched."

http://developer.apple.com/iphone/library/samplecode/DrillDownSave

Steph Thirion
A: 

I've published a little library to help doing these kinds of things. I'm already using it in an app I've published on the App Store and it's a lot smoother than implementing NSCoder for every view manually :P

boxed