学习归档

时间:2013-08-15 00:31:55

标签: iphone ios objective-c

我正在尝试保存并加载简单应用程序的设置。然而我仍然是obj-c的新手。我相信保存部分正在工作,但不是装载部分。当我更改设置时,按返回并重新进入设置视图,未加载设置。

我认为这与使用保存的数据创建SettingsViewController的方式有关,但我无法绕过它。

以下是来自SettingsViewController的有趣代码,以及下面的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];
}

MainWindowViewController中的代码初始化settingsViewController:

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

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

1 个答案:

答案 0 :(得分: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" ] ];