开发者

iPhone app with remember last user action

开发者 https://www.devze.com 2022-12-20 05:38 出处:网络
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 .

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 ?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消