NSSlider event not firing
I have got an event on my NSSlider with this code:
- (IBAction)optOndoorzichtigheidChange:(id)sender {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *opacity = [NSString stringWithFormat:@"%d",[optOndoorzichtigheidSlider value]];
[defaults setOb开发者_JAVA技巧ject:opacity forKey:@"opacity"];
[mainWindow setAlphaValue:[optOndoorzichtigheidSlider doubleValue]];
[defaults synchronize];
[optOndoorzichtigheidLabel setStringValue:opacity];
NSLog(@"fired");
}
But it's not firing and the console gives this message: 2011-01-09 19:31:18.994 Nistract[1337:a0f] -[NSSlider value]: unrecognized selector sent to instance 0x100427400
The method is executing. You're just crashing because you're trying to execute a method that doesn't exist.
The problem is this line:
NSString *opacity = [NSString stringWithFormat:@"%d",[optOndoorzichtigheidSlider value]];
NSSlider
does not have a value
method. It has a doubleValue
method that returns a double
.
You've hard-coded your slider. Instead of calls like this
[optOndoorzichtigheidSlider intValue];
try using the sender
[sender intValue];
And it looks like you are updating a label with the value here
[optOndoorzichtigheidLabel setStringValue:opacity];
When you could consider using Bindings instead.
Now I use this code and it seems to work:
- (IBAction)optOndoorzichtigheidChange:(id)sender {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *opacity = [NSString stringWithFormat:@"%d",[optOndoorzichtigheidSlider intValue]];
[defaults setObject:opacity forKey:@"opacity"];
[[mainWindow animator] setAlphaValue:((double) [optOndoorzichtigheidSlider intValue] / 100)];
[defaults synchronize];
[optOndoorzichtigheidLabel setStringValue:opacity];
NSLog(@"fired");
}
精彩评论