我正在使用NSMutableArray
存储来自我的数据UITableView
,并且我想将数据存储在NSUserDefaults
应用程序didEnterBackground
以及willTerminate
. 到目前为止,我是这样做的:
代表:
- (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"];
}
main.data
是一个NSMutableArray
。
我忘记了如何做到这一点,因为我已经有一段时间没有使用表格视图了。任何帮助表示赞赏,谢谢!
完整的委托代码 (.h):
#import <UIKit/UIKit.h>
@class EXViewController;
@interface EXAppDelegate : UIResponder <UIApplicationDelegate> {
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
和(.m):
- (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;
}