0

I have set up state restoration so that my application retains user input, button states, etc. on background/terminate/restart. I am saving a bunch of them, so I will only include partial clode for what this looks like in my viewcontroller.m file:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    [coder encodeObject:_startLevel.text forKey:@"startText"];

    // stop level text
    [coder encodeObject:_stopLevel.text forKey:@"stopText"];
}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    _startLevel.text = [coder decodeObjectForKey:@"startText"];

    // stop level text
    _stopLevel.text = [coder decodeObjectForKey:@"stopText"];
}

This works perfectly with all of my user input saved on BG/T/Restart.

I have also set up NSUserDefaults in order to keep user input data for all of the same fields when going back and forth between my main menu and this view.

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // start level
    [[NSUserDefaults standardUserDefaults] setObject:_startLevel.text forKey:@"startLevelRestore"];

    // stop level
    [[NSUserDefaults standardUserDefaults] setObject:_stopLevel.text forKey:@"stopLevelRestore"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];

        // start level
        [_startLevel setText:[[NSUserDefaults standardUserDefaults objectForKey:@"startLevelRestore"]];

        // stop Level
        [_stopLevel setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stopLevelRestore"]];

[[NSUserDefaults standardUserDefaults] synchronize];

    }

This also works. User input persists going back and forth between my menu view and this viewcontroller.

Unfortunately, using NSUserDefaults in this way seems to override viewDidLoad, which I have been using to set the initial state of this view, which is fine on every occasion OTHER than the very first launch of the application following installation. My buttons are not set in the proper states and the placeholders for my labels do not appear.

So I found this great solution here on StackOverflow that shows how to set initial values for NSUserDefaults using a Property List. I created the PList, and put this code in my appdelegate.m file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *defaultPrefsFile = [[NSBundle mainBundle] pathForResource:@"defaultPrefs" ofType:@"plist"];
    NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfFile:defaultPrefsFile];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultPreferences];

    return YES;
}

This does indeed set my user inputs correctly to their desired states, but unfortunately, it kills the functionality of my state restoration during background/termination/restart. Essentially, during that process, all of my initial settings are refreshed to their initial states - text fields cleared, buttons re-set to their initial enable settings, etc.

I have been trying to figure this out all night with no luck, and I get the feeling that someone more seasoned than myself will have a pretty easy fix that I am just not seeing. I appreciate any help. If I can provide any more info, please advise and I will add code to this OP.

Thanks!!!

Community
  • 1
  • 1
C_Dub
  • 77
  • 11
  • Why don't you check for first start and that way recognize if you have to restore from user defaults or just set initial values? – Volker Feb 15 '14 at 10:17
  • Can you show us the content of your plist? – Enrico Susatyo Feb 15 '14 at 10:18
  • @EnricoSusatyo I don't see how to copy and paste it, but the values all work. – C_Dub Feb 15 '14 at 10:20
  • @Volker I thought about that and there are a bunch of different solutions for that on this forum, but I am unsure how to make them work. It seemed to me there was a more straightforward solution seeing as how almost every app out there that takes user input needs that data to persist across both scenarios above. This seems to be the favorite solution, but I am unsure how to flesh out the if statement. http://stackoverflow.com/a/9964400/2872997 – C_Dub Feb 15 '14 at 10:24
  • This shouldn't make a difference, but can you try this just to make sure everything's ok: http://stackoverflow.com/q/4931167/361247 – Enrico Susatyo Feb 15 '14 at 10:26
  • Te linked question is exactly what I am thinking about. Just check on startup if user defaults have the key @"firstLaunch" and if not, it is the real Obrist start. Then don't do any restore code and set a value in user defaults for the key. – Volker Feb 15 '14 at 10:30
  • FYI I am using exactly that code pretty much in a lot of my apps and they all work perfectly. – Enrico Susatyo Feb 15 '14 at 10:31
  • When you say "don't do any restore code", I don't see what you mean. Don't I need all of the restore code in my view controller for when the user goes back and forth to menu from my view controller? – C_Dub Feb 15 '14 at 10:35

0 Answers0