I am trying to store values in settings bundle child pane.
I have the Root.plist and the child moteurs.plist
In the moteurs.plist, I have 14 motors and I’d like to store 2 values (calibration coefficients) for each motor.
Those values need to be alterable by the user.
In the Root.plist I have :
<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>
In the 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>
I faced a problem:
I am unable to initialize defaults values for the child ( or I don’t know how to read the values )
Root default values are initialized using this:
- (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 can access root preferences values using
NSString *test = [[NSUserDefaults standardUserDefaults] stringForKey:@"serveur_preference"];
NSLog(@"serveur:%@",test);
So how can I read child pane preference values ?
And what’s best practice to store the 2 coefficients for each motor?
Thanks a lot.
The child pane settings should be accessible using their usual identifiers (as specified in the plist), like so
likewise, you can set the defaults using
-NSUserDefaults registerDefaults: