1

我正在尝试将值存储在设置捆绑子窗格中。

我有Root.plist和孩子moteurs.plist

在 中moteurs.plist,我有 14 个电机,我想为每个电机存储 2 个值(校准系数)。这些值需要由用户更改。

Root.plist我有:

<dict>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Title</key>
        <string>Moteurs</string>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSChildPaneSpecifier</string>
        <key>Title</key>
        <string>Paramètres moteurs</string>
        <key>File</key>
        <string>moteurs</string>
        <key>Key</key>
        <string>moteurs</string>
    </dict>
    <dict>

moteurs.plist

<dict>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Title</key>
        <string>L5K-070</string>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
    </dict>
    <dict>
        <key>AutocapitalizationType</key>
        <string>None</string>
        <key>AutocorrectionType</key>
        <string>No</string>
        <key>DefaultValue</key>
        <string>0,0</string>
        <key>IsSecure</key>
        <false/>
        <key>Key</key>
        <string>coeff1_preference</string>
        <key>KeyboardType</key>
        <string>Number Pad</string>
        <key>Title</key>
        <string>Coeff 1</string>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
    </dict>
    <dict>
        <key>AutocapitalizationType</key>
        <string>None</string>
        <key>AutocorrectionType</key>
        <string>No</string>
        <key>DefaultValue</key>
        <string>0,0</string>
        <key>IsSecure</key>
        <false/>
        <key>Key</key>
        <string>coeff2_preference</string>
        <key>KeyboardType</key>
        <string>Number Pad</string>
        <key>Title</key>
        <string>Coeff 2</string>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
    </dict>

我遇到了一个问题:我无法为孩子初始化默认值(或者我不知道如何读取这些值)

使用以下方法初始化根默认值:

- (void)registerDefaultsFromSettingsBundle {
    [[NSUserDefaults standardUserDefaults] registerDefaults:[self defaultsFromPlistNamed:@"Root"]];
     [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSDictionary *)defaultsFromPlistNamed:(NSString *)plistName {
    NSLog(@"Traitement de %@:",plistName);

    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    NSAssert(settingsBundle, @"Could not find Settings.bundle while loading defaults.");

    NSString *plistFullName = [NSString stringWithFormat:@"%@.plist", plistName];

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:plistFullName]];
    NSAssert1(settings, @"Could not load plist '%@' while loading defaults.", plistFullName);

    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    NSAssert1(preferences, @"Could not find preferences entry in plist '%@' while loading defaults.", plistFullName);

    NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        id value = [prefSpecification objectForKey:@"DefaultValue"];
        if(key && value) {
            [defaults setObject:value forKey:key];
        }

        NSString *type = [prefSpecification objectForKey:@"Type"];
        if ([type isEqualToString:@"PSChildPaneSpecifier"]) {
            NSString *file = [prefSpecification objectForKey:@"File"];
            NSAssert1(file, @"Unable to get child plist name from plist '%@'", plistFullName);
            [defaults addEntriesFromDictionary:[self defaultsFromPlistNamed:file]];
        }
    }

    return defaults;
}

我可以使用访问根首选项值

NSString *test = [[NSUserDefaults standardUserDefaults] stringForKey:@"serveur_preference"];
    NSLog(@"serveur:%@",test);

那么如何读取子窗格首选项值?为每个电机存储 2 个系数的最佳做法是什么?

非常感谢。

4

1 回答 1

0

子窗格设置应该可以使用它们的常用标识符(如 plist 中指定的那样)访问,如下所示

NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
NSString* coeff1 = [settings stringForKey:@"coeff1_preference"];
NSString* coeff2 = [settings stringForKey:@"coeff2_preference"];

同样,您可以使用设置默认值-NSUserDefaults registerDefaults:

于 2012-12-29T12:05:44.587 回答