在NSUserDefaults iOS

时间:2015-05-30 13:49:40

标签: ios objective-c nsuserdefaults

我使用以下代码从我的AppDelegate将int和其他一些内容保存到NSUserDefaults中。我保存的另一个阵列中充满了符合NSCoding的对象,因此我不认为这是一个问题。如果当前位置不为零,则表示正在进行会话,因此将加载所有数据。保存数据时,只有在当前位置不为零时才会保存,这表示正在进行会话。当我退出物理设备上的应用程序时,我知道此调用,因为NSLog消息出现在调试器中。

- (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.

if (self.currentPlace != 0) {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arr = self.places; // set value
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
    [defaults setObject:data forKey:@"places"];
    [defaults setInteger:self.currentPlace forKey:@"current"];
    NSLog(@"Saving and Quitting b/c we have a place verified!");
    [defaults synchronize];
}
}

- (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.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSInteger myInt = [defaults integerForKey:@"current"];

self.currentPlace = (int)myInt;

if (self.currentPlace != 0) {
    NSData *data = [defaults objectForKey:@"places"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    self.places = [arr mutableCopy];
    NSLog(@"Current Place: %i",self.currentPlace);
}

}

我正在使用AppDelegate来存储可以从我的应用中的多个屏幕访问的数据。在第一个ViewController中,向用户显示一个菜单。如果appDelegate的currnentPlace值 0,则会显示继续从AppDelegate加载的数据的选项。但是,从未提供此选项。我在第一个视图控制器中使用以下内容检查currentPlace int的值:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// Do any additional setup after loading the view, typically from a nib.
[self setupViews];
self.spinner.alpha = 0;
NSLog(@"Current %i",delegate.currentPlace);

if (delegate.currentPlace == 0) {
    self.continueButton.enabled = false;
    self.continueButton.alpha = 0.5;
}
else if (delegate.currentPlace != 0) {
    self.continueButton.enabled = true;
    self.continueButton.alpha = 1;
}

如果有人能够看到我做错了什么,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

您需要使用

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

在你正在制作AppDelegate类对象的控制器中。

如果仍然无法获得当前值,那么只需将用户默认值存储为字符串,当您访问它时,将其转换为您想要的适当数据类型。这可能对你有帮助。

答案 1 :(得分:1)

将数据恢复代码块移动到方法中,如下所示:

   - (void)restoreData {
       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
       NSInteger myInt = [defaults integerForKey:@"current"];

       self.currentPlace = (int)myInt;

       if (self.currentPlace != 0) {
         NSData *data = [defaults objectForKey:@"places"];
         NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
         self.places = [arr mutableCopy];
         NSLog(@"Current Place: %i",self.currentPlace);
       }
    }

然后在application:didFinishLaunchingWithOptions:中的applicationWillEnterForeground:AppDelegate方法中调用此方法。