1

I need to set a slider in preferences window of a cocoa application.

If I set the NSSlider in awakeFromNib like so

-(void)awakeFromNib{

[thresholdSlider setInValue:9];

}

the preference window updates with the value when opened.

Though, since it is a preference window, I need to register the Value with NSUserDefault, so when the application at launch would run :

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

[thresholdSlider setValue:[NSUserDefaults  standardUserDefaults] forKey:kthresh];
NSLog( @"%@",[thresholdSlider objectValue]);

}

But I cant even set the slider value in the applicationDidFinishLaunching method

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

[thresholdSlider setIntValue:9];

NSLog( @“%d”,[thresholdSlider intValue]);}

returns 0 and slider is set to minimum value(set in IB) in the preferences window.

Where can I call the [thresholdSlider setValue:[NSUserDefaults standardUserDefaults] forKey:kthresh]; to get the slider updated with the user value on last application quit?

The code edited according to Vadian proposition :

+(void)initialize{
NSDictionary *dicDefault = @{@"kthresh":@9};
[[NSUserDefaults standardUserDefaults]registerDefaults:dicDefault];}`

`- (void)applicationDidFinishLaunching:(NSNotification*)aNotification{   
  `//Preferences
NSInteger thresholdValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"kthresh"];`

thresholdSlider.integerValue = thresholdValue;}`

`-(void)applicationWillTerminate:(NSNotification *)notification {
NSUserDefaults *defaults =  [NSUserDefaults standardUserDefaults];
[defaults setInteger:thresholdSlider.integerValue forKey:@"kthresh"];
[defaults synchronize];}` 
user306147
  • 27
  • 1
  • 6

2 Answers2

1

In AppDelegate as soon as possible register the key value pair with a default value. This value is always used if no custom value has been written to disk yet.

NSDictionary *defaultValues = @{@"kthresh" : @9};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

Then set the value of the slider wherever you want, consider the syntax

NSInteger thresholdValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"kthresh"];
thresholdSlider.integerValue = thresholdValue;

To write the value to disk use this

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults] ;
[defaults setInteger:thresholdSlider.integerValue forKey:@"kthresh"];
[defaults synchronize];

Do not use setValue:forKey: and valueForKey: to talk to NSUserDefaults

Alternatively use Cocoa bindings and bind the key integerValue to the NSUserDefaultsController instance in Interface Builder.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • The cocoa binding solution works, but the code does not , I edited my answer showing your solution so you can see where I call the functions. Although thank you I was also forgetting to call ` synchronise`! – user306147 Dec 12 '15 at 19:53
  • 1
    Actually the code to register the default value(s) can be also in `applicationDidFinishLaunching`, it must be ensured that it's executed before retrieving the values the first time. In `+initialize` you have to check for the current class e.g. `if (self == [AppDelegate class]) {` because `+initialize` is called multiple times. The code to read and write from and to `NSUserDefaults` is standard code and is supposed to work. – vadian Dec 12 '15 at 20:14
  • Well finally found out the issue was coming from the xib file not from the code. it works now, both ways. – user306147 Dec 13 '15 at 03:33
0

[thresholdSlider setValue:[NSUserDefaults standardUserDefaults] forKey:kthresh];

should be

[thresholdSlider setObjectValue:[[NSUserDefaults standardUserDefaults] valueForKey:kthresh]];

Willeke
  • 14,578
  • 4
  • 19
  • 47
  • Thank you indeed my declaration was not right. Though still not working as I mentioned in the question, even if not using`NSUserDefaults` the fonction [thresholdSlider setIntValue:9] is only changing the slider value if put in awakeFromNIb{} not in applicationDidFinishLaunching{}. Though I have other functions in applicationDidFinishLaunching, and they all get called properly. Im puzzled. – user306147 Dec 12 '15 at 18:32