我正在尝试将值存储在设置捆绑子窗格中。
我有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 个系数的最佳做法是什么?
非常感谢。