I need my app to remember which was the last UIViewController opened so when the app is loaded out of memory, I substitute a rootViewController property in the AppDelegate with one saved in NSUserDefaults:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
frontViewController = [[SGLoginScreenViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
What I want to do is to put some code in the ViewDidLoad method of each ViewController and remember its name in NSUserDefaults to later use it in didFinishLaunchingWithOptions, something like this:
[[NSUserDefaults standardUserDefaults] setObject:self forKey:@"currentViewController"];
[[NSUserDefaults standardUserDefaults] synchronize];
The problem is, however, that XCode is giving me this warning:
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<SGMainScreenFirstTimeViewController: 0x8e1fea0>' of class 'SGMainScreenFirstTimeViewController'. Note that dictionaries and arrays in property lists must also contain only property values.
Any ideas on how to implement this whole thing right? Thanks in advance
You can only save in user defaults objects that can conform to the Key-Value coding (NSString, NSnumber, etc). Converting class to string and recreating class knowing class name would be the way to go for what you wish to achieve.
This would be a good approach for saving in NSUserDefaults
And for loading back you recreate the class based on the saved string, as below: