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!!!