Chris S
Chris S

Reputation: 65476

Loading the last viewed UIViewController when an app quits

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.

Upvotes: 1

Views: 356

Answers (2)

James Eichele
James Eichele

Reputation: 119214

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
}

Upvotes: 3

choise
choise

Reputation: 25254

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.

Upvotes: 1

Related Questions