1

Working on a tutorial with a NSSlider.

What I want: Moving the slider will show slider value in NSTextField.

Tutorial explains that following method will show slider value in text field:

- (IBAction)sliderDidMove:(id)sender {
    NSSlider *slider = sender;
    double value = [slider doubleValue];
    [sliderValueLabel setDoubleValue:value];    
}

Method does not work so I tried to find the method declaration on Apples developer website but couldn't find it. From my understanding is the method: sliderDidMove a class method from the class NSSlider so why I'm unable to find any documentation?

  • Where I was looking for the method: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSlider_Class/Reference/Reference.html –  Feb 19 '12 at 15:15

2 Answers2

1

When slider value changes, it sends -[NSControl action] to its -[NSControl target]. So in Interface Builder you need to Ctrl-drag from the slider to the object that has sliderDidMove: (that will probably be either App Delegate or File's Owner). The name is chosen by the tutorial's author, it can be anything else.

Alternatively, you can set it up programmatically:

[slider setTarget:self]; // assume the handler is [self sliderDidMove:]
[slider setAction:@selector(sliderDidMove:)];

Note also that this particular task is better solved with bindings: bind both slider's and label's value to a double property of some object, and Cocoa will keep them synchronized.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
0

In Swift ...

 // Somewhere maybe in viewDidLoad ...
        slider.target = self
        slider.action = #selector(sliderDidMove)



// Later on..

func sliderDidMove(){
    print("The slider moved!")
}
Andre Yonadam
  • 964
  • 1
  • 13
  • 30