0

我正在使用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;
}
4

3 回答 3

1

您每次都在分配和初始化 EXViewController 的新实例 - 您可能应该在应用程序委托中引用单个实例,然后保存/恢复该数据。

编辑

// In your AppDelegate.m - a lot of code ommitted
// NOTE: make sure that self.viewController points to self->_main
// This must be in the @implementation block
@synthesize viewController = _main;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // NOTE: Notice the removal of the _main = ...
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    // NOTE: This is the only view controller you should reference.
    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;
}

- (void)applicationWillTerminate:(UIApplication *)application {
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     [[NSUserDefaults standardUserDefaults] setObject:self.viewController.data forKey:@"dataKey"];
}

// You will also need to restore it when needed
于 2013-05-25T19:58:11.073 回答
-1
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

[[NSUserDefaults standardUserDefaults] setObject:self.data forKey:@"dataKey"];

[[NSUserDefaults standartUserDefaults] syncronize];

}

数据是一种属性。

于 2013-05-25T19:54:15.790 回答
-1

保存

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// 保存一个 NSString [prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// 保存一个 NSInteger [prefs setInteger:42 forKey:@"integerKey"];

// 保存一个 Double [prefs setDouble:3.1415 forKey:@"doubleKey"];

// 保存一个浮点数 [prefs setFloat:1.2345678 forKey:@"floatKey"];

// 建议同步首选项,但不是必需的(我没有把它放在我的 tut 中)[首选项同步];

检索

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// 得到一个 NSString NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// 得到一个 NSInteger NSInteger myInt = [prefs integerForKey:@"integerKey"];

// 得到一个浮点数 float myFloat = [prefs floatForKey:@"floatKey"];

于 2013-05-25T19:56:42.587 回答