开发者

How to save Application State using NSUserDefaults in iPhone?

开发者 https://www.devze.com 2023-01-02 10:56 出处:网络
Following is what i am doing: - (void) applicationWillTerminate: (UIApplication*) application { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Following is what i am doing:

- (void) applicationWillTerminate: (UIApplication*) application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:navigationController.viewControllers
        forKey:@"diabetesstate"];
}

- (void) applicationDidFinishLaunching: (UIA开发者_StackOverflow社区pplication*) application {
    NSMutableArray *state = [[NSUserDefaults standardUserDefaults] 
        objectForKey:@"diabetesstate"];
    if (state == nil) {
        //Will initialize a new controller and put it as root view controller
    }
    else {
        [navigationController setViewControllers:state animated:NO];
    }
}


It looks like you're trying to add a UIViewController to the userdefaults? I doubt that will work.

I guess you'll have to put some identifer string or number in there that tells you which viewcontroller is currently displayed, and when the application starts basically check that value and set up your viewcontrollers accordingly.

I need to implement something similar for my application. I've got a list of objects, and when the user taps on one I am displaying a child object. My idea is to store the ID of the client object if one is currently being displayed, otherwise NULL. When the application starts I will check the value. If it's NULL I'll display the list of parent objects, otherwise I'll show the child object with the ID that's in userdefaults.

0

精彩评论

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

关注公众号