设置默认值
我正在尝试将值存储在设置捆绑子窗格中。
我有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>
我面临一个问题:我无法初始化孩子的默认值(或者我不知道如何读取值)
使用以下来初始化root默认值:
- (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;
}
i可以使用
访问root首选项值NSString *test = [[NSUserDefaults standardUserDefaults] stringForKey:@"serveur_preference"];
NSLog(@"serveur:%@",test);
那么如何读取儿童窗格偏好值?对于每个电动机存储2个系数的最佳做法是什么?
非常感谢。
应该使用其通常的标识符(如PLIST中指定的)访问儿童窗格设置,例如So
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
NSString* coeff1 = [settings stringForKey:@"coeff1_preference"];
NSString* coeff2 = [settings stringForKey:@"coeff2_preference"];
同样,您可以使用-NSUserDefaults registerDefaults: