2

Im trying to save and load the settings of a simple application. However im still new to obj-c. I do believe the saving part is working, but not the loading part. When i change the settings, press back and reenter the settingsview, the settings are not loaded.

I think it's something with how the SettingsViewController is created with the saved data, but i cannot wrap my head around it.

Below is the interesting code from the SettingsViewController, and further below, from the MainWindowViewController

SettingsViewController:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        NSString * path = [self settingsArchivePath];

        self = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        shakeSwitch.on = shakeForTag;
        [warningDistanceFld setText:[NSString stringWithFormat:@"%i",warningDistance]];

}
return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithNibName:@"SettingsViewController" bundle:nil];
    if(self)
    {
        [self setShakeForTag:[aDecoder decodeBoolForKey:@"shakeForTag"]];
        [self setWarningDistance:[aDecoder decodeIntForKey:@"warningDistance"]];
        [self setMapType:[aDecoder decodeIntegerForKey:@"mapType"]];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInt:warningDistance forKey:@"warningDistance"];
    [aCoder encodeBool:shakeForTag forKey:@"shakeForTag"];
    [aCoder encodeInteger:mapType forKey:@"mapType"];
}

-(NSString *)settingsArchivePath
{
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString * documentDirectory = [documentDirectories objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"settings.archive"];
}

-(IBAction)back:(id)sender
{

    if([mapChanger selectedSegmentIndex] == 0){
        mapType = MKMapTypeSatellite;
    }else if([mapChanger selectedSegmentIndex] ==1){
        mapType = MKMapTypeHybrid;
    }else{
        mapType = MKMapTypeStandard;
    }

    NSString *warning = [warningDistanceFld text];
    warningDistance = [warning intValue];
    shakeForTag =shakeSwitch.on;

    [self saveChanges];

    [self dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)saveChanges
{
    NSString *path = [self settingsArchivePath];
    NSLog(@"saved");
   return [NSKeyedArchiver archiveRootObject:self toFile:path];
}

Code in MainWindowViewController to initialize the settingsViewController:

svc = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];

svc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:svc animated:YES completion:nil];
4

1 回答 1

1

使用NSKeyedArchiver保存简单应用程序的设置有点矫枉过正。

Foundation 框架提供了NSUserDefaults类,它更易于使用。
对于 iOS 或 OS X,这是要走的路。

它将plist使用您的设置将文件写入正确的位置。

写东西基本上是:

[ [ NSUserDefaults standardUserDefaults ] setBool: YES forKey: @"SettingsKey" ];
[ [ NSUserDefaults standardUserDefaults ] setInteger: 42 forKey: @"SettingsKey" ];
[ [ NSUserDefaults standardUserDefaults ] synchronise ];

最后一行确保数据写入磁盘。它不是强制性的,因为它会定期自动调用,但显式调用它通常更安全。

获取数据基本上是:

[ [ NSUserDefaults standardUserDefaults ] boolForKey: @"SettingsKey" ];
[ [ NSUserDefaults standardUserDefaults ] integerForKey: @"SettingsKey" ];

NSUserDefault可以管理很多数据类型。整数、布尔值、浮点数,还有数组 ( NSArray)、字典 ( NSDictionary)、数据 ( NSData) 等。

另请注意,您可以从现有plist文件(即在您的应用程序资源中)加载默认设置:

[ [ NSUserDefaults standardUserDefaults ] registerDefaults: [ NSDictionary dictionaryWithContentsOfFile: @"path/to/plist" ] ];
于 2013-08-15T01:34:26.453 回答