SettingsViewController

SettingsViewController

我正在尝试保存和加载简单应用程序的设置。但是我对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];

最佳答案

使用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" ] ];

关于iphone - 学习存档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18244536/

10-09 15:35