Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to save current data in an NSMutableArray on exit, and load it on launch
    text
    copied!<p>I'm using an <code>NSMutableArray</code> to store data from my <code>UITableView</code>, and I'd like to store the data in <code>NSUserDefaults</code> on the application <code>didEnterBackground</code> as well as <code>willTerminate</code>. Here's how I'm doing this so far:</p> <p>Delegate:</p> <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. EXViewController *main = [[EXViewController alloc] init]; [[NSUserDefaults standardUserDefaults] setObject:main.data forKey:@"dataKey"]; } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. EXViewController *main = [[EXViewController alloc] init]; [[NSUserDefaults standardUserDefaults] setObject:main.data forKey:@"dataKey"]; } </code></pre> <p><code>main.data</code> is an <code>NSMutableArray</code>.</p> <p>I forgot how to do this, being the fact that I haven't worked with table views in some time. Any help is appreciated, thanks!</p> <p>Full delegate code (.h):</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class EXViewController; @interface EXAppDelegate : UIResponder &lt;UIApplicationDelegate&gt; { EXViewController *_main; NSString *title; } // Data to be added @property (nonatomic, retain) NSString *title; // Default properties @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) EXViewController *viewController; @end </code></pre> <p>And (.m):</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _main = [[EXViewController alloc] init]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[EXViewController alloc] initWithNibName:@"EXViewController" bundle:nil]; // Create navigation controller UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload