Save the current status when quit, auto-reload when re-open the app

前端 未结 2 939
刺人心
刺人心 2020-12-20 02:14

I\'m creating a simple app to simply save a person\'s name and a number associated with this person. I\'ve created a class for person like this:

@interface P         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 02:53

    Easy to do with NSUserDefaults, here's some code to get you started:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        appDelegate.lastPerson.name = [defaults stringForKey:@"Name"];
        appDelegate.lastPerson.serial = [defaults stringForKey:@"Serial"];
    
        [window addSubview:[flipViewController view]];
        [window makeKeyAndVisible];
    }
    
    -(void)applicationWillTerminate:(UIApplication *)application {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setString:appDelegate.lastPerson.name forKey:@"Name"];
        [defaults setString:appDelegate.lastPerson.serial forKey:@"Serial"];
        [defaults synchronize];     
    }
    

    You can check if either of the strings are nil after loading which will indicate no stored value and you should then ask the user for input.

提交回复
热议问题