views:

70

answers:

2

Is there any in-built technique for moving straight to the last UIViewController shown before quiting your app?

I'm trying to figure out if there are any inbuilt ways of doing this with the UIKit framework /Cocoa Touch. If not I realise it will be fairly trivial to implement, but I don't want to do this if something already exists.

For example:

  1. You start your app
  2. You move through 3 UIViewControllers (inside a UINavigationController)
  3. You move through a list of items each one is displayed full screen, to item 5
  4. You quit

You then restart the app and want to be back on that last view you were at step 3 - viewing your fifth item.

+2  A: 

NSUserDefaults will be the tool for this job. You could simply save a key to indicate your position in the app, and then read this key again when the app launches to decide where to go:

// whenever you move to a new screen or item:
[[NSUserDefaults standardUserDefaults] setObject:viewIdentifierString
                                          forKey:@"last_view"];


// when your application opens:
NSString * viewIdentifierString = [[NSUserDefaults standardUserDefaults]
                                     stringForKey:@"last_view"];

if (viewIdentifierString == nil)
{
    // show default view
}
else
{
   // use viewIdentifierString to determine which view to load
}
e.James
This is how I was planning on doing it, with each controller having a key. I was hoping there was a lazyier way of doing it than pushing based on the keys though
Chris S
I'm afraid not. Since every app can have a different way of loading its views, it becomes very difficult for Cocoa to have a default method of drilling down into the view hierarchy.
e.James
+1  A: 

you can store your last viewed viewcontroller to a NSUserDefaults for example. then in your app delegate read out this value and load the appointed view controller.

overwrite your NSUserDefaults value on every init of your viewcontrollers or when your app gets terminated.

choise