tags:

views:

186

answers:

2

hi i want create an application and my app remember last user action ,, it means user find himself in the same page as he was last time he ran the application . how could implement that ?

+1  A: 

Use the Application Delegate and override the method:

- (void)applicationWillTerminate:(UIApplication *)application

In here you can save which view was last visible and what state it was in (and if necessary other views as well) and persist it.

Then when you start your application again you load the saved state and use it to determine which view should be visible.

There are a few ways to persist data in the iPhone SDK so please read this guide for more information on how to do that.

willcodejavaforfood
+1  A: 

Hi, you can use the NSUserDefaults to store some variable for your application to retrieve them after a close of the application. For exemple:

// Set the name of the view
[[NSUserDefaults standardUserDefaults] setObject:@"viewName" forKey:@"lastViewName"];

To retrieve the last action of the user:

// Retrieve the last view name
NSString *lastViewName = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastViewName"];

But you can add other than a string.


Edit:

define some constant like that on a header file:

#define HOME_VIEW 0
#define VIEW1 1
#define VIEW2 2

When a view is load you store the current constant view to your standardUserDefaults. For example the view1:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the name of the view
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:VIEW1] forKey:@"lastView"];

}

When you application did load you retrieve your constant and you load your appropriate view:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
   // Retrieve the last view name
   NSInteger constantView = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastView"];

   switch(constantView) {
      case HOME_VIEW : //load your UIViewController
          break;
      case VIEW1 : // load your VIEW1
          break;
      //...
   }
}

I haven't tested this code, but it's something like that to do.

Yannick L.
thank you .. but have some problem !is viewName my nib view? and what can i do if i want load from any view , and should this code on applicationDidFinishLaunching ? and xcode compiler tells me that lastViewName is un used variable ! sorry iam new to iphone sdk
Momeks
NSUserDefaults allows you to store object which are an instance of NSData, NSString, NSNumber, NSDate, NSArray or NSDictionary.So you can store the string of your nibname or another constant. When your application start you retrieve the string or the constant and you must use it to load your UIViewController.The lastViewName is an unused variable because you must use it in your code to load the appropriate ViewController.
Yannick L.
thank you Yannick , but can you edit your post and write whole of code? on the .h and .m ?
Momeks
I have edited my post. I hope it'll help you.
Yannick L.
thank you , i tried but doesn't work ! :( . EDIT : i download DrillDownSave sample code . and take a look
Momeks