0

NSMutableArrayからのデータを保存するためにを使用していますが、データを だけでなくアプリケーションにもUITableView保存したいと考えています。これまでのところ、これを行う方法は次のとおりです。NSUserDefaultsdidEnterBackgroundwillTerminate

代理人:

- (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 に答える