views:

430

answers:

3

Can someone a good way to save the program state (UINavigationController stack, etc) of an iPhone application.

My application obtains a bunch of information from the network and I want to return the person back to the last screen they were on, even if it was 3 or 4 screens deep.

I assume that I will need to reload the data from the network along the way as I recreate the UINavigation controllers. I don't necessarily have a problem with this.

I'm thinking about maybe having my UINavigationController objects implement some type of protocol which allow me to save/set their state? I'm looking to hear from others who may have needed to implement a similar scenario and how they accomplished it.

My application has a UITabbarController at the root and UINavigationController items for each tab bar item.

thanks!

A: 

Hi, I did this in one of my apps and from what I can see its quite a manuagel process

For each of your TargetViewControllers you will have to push them onto the view controller, set animated to NO so it looks like it is restoring state.

[[self navigationController] pushViewController:targetViewController animated:NO];

Maybe someone else can point you to a framework that allows you to persist these view controllers.

John Ballinger
+1  A: 

Here is what I ended up doing as a solution.

I created an protocol which contains a method to "obtain state" and then to "init with state from dictionary method".

So, when the application closes, I loop through all my controllers and ask them for state. And then when application starts back up, I do init with state, passing in the dictionary I serialized. It works well!

jr
A: 
JRo
Are you sure this is a viable approach? It seems as if you are storing UIViewControllers in NSUserDefaults. As far as I know, you can only store .plist objects in NSUserDefaults. Also, it may be much more complex for other apps in which variables need to be stored and retrieved as well. Working with an NSDictionary (stored in NSUserDefaults) is your best option in my opinion.
bare_nature