菜鸟教程小白 发表于 2022-12-12 13:36:53

iphone - 学习归档


                                            <p><p>我正在尝试保存和加载一个简单应用程序的设置。但是我对 obj-c 还是新手。我确实相信保存部分正在工作,但不是加载部分。当我更改设置时,按返回并重新进入设置 View ,设置未加载。 </p>

<p>我认为这与使用保存的数据创建 SettingsViewController 的方式有关,但我无法理解它。</p>

<p>下面是来自 SettingsViewController 的有趣代码,下面是来自 MainWindowViewController 的代码</p>

<p>设置 ViewController :</p>

<pre><code>-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = ;
    if(self)
    {
      NSString * path = ;

      self = ;
      shakeSwitch.on = shakeForTag;
      ];

}
return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = ;
    if(self)
    {
      ];
      ];
      ];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    ;
    ;
    ;
}

-(NSString *)settingsArchivePath
{
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString * documentDirectory = ;
    return ;
}

-(IBAction)back:(id)sender
{

    if( == 0){
      mapType = MKMapTypeSatellite;
    }else if( ==1){
      mapType = MKMapTypeHybrid;
    }else{
      mapType = MKMapTypeStandard;
    }

    NSString *warning = ;
    warningDistance = ;
    shakeForTag =shakeSwitch.on;

    ;

    ;
}

-(BOOL)saveChanges
{
    NSString *path = ;
    NSLog(@&#34;saved&#34;);
   return ;
}
</code></pre>

<p>MainWindowViewController 中用于初始化 settingsViewController 的代码:</p>

<pre><code>svc = [initWithNibName:@&#34;SettingsViewController&#34; bundle:];

svc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用 <code>NSKeyedArchiver</code> 来保存简单应用程序的设置有点矫枉过正。</p>

<p>Foundation 框架提供了 <strong> <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html" rel="noreferrer noopener nofollow"><code>NSUserDefaults</code></a> </strong> 类,使用起来更方便。<br/>
这是适用于 iOS 或 OS X 的方法。</p>

<p>它将使用您的设置在正确的位置写入一个 <code>plist</code> 文件。</p>

<p>写东西基本上是:</p>

<pre><code>[ [ NSUserDefaults standardUserDefaults ] setBool: YES forKey: @&#34;SettingsKey&#34; ];
[ [ NSUserDefaults standardUserDefaults ] setInteger: 42 forKey: @&#34;SettingsKey&#34; ];
[ [ NSUserDefaults standardUserDefaults ] synchronise ];
</code></pre>

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

<p>获取数据基本上是:</p>

<pre><code>[ [ NSUserDefaults standardUserDefaults ] boolForKey: @&#34;SettingsKey&#34; ];
[ [ NSUserDefaults standardUserDefaults ] integerForKey: @&#34;SettingsKey&#34; ];
</code></pre>

<p><code>NSUserDefault</code> 可以管理很多数据类型。整数、 bool 值、 float ,还有数组(<code>NSArray</code>)、字典(<code>NSDictionary</code>)、数据(<code>NSData</code>)等</code> p>

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

<pre><code>[ [ NSUserDefaults standardUserDefaults ] registerDefaults: [ NSDictionary dictionaryWithContentsOfFile: @&#34;path/to/plist&#34; ] ];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 学习归档,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18244536/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18244536/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 学习归档