Required NSSlider method (sliderDidMove) declaration not found on Apple's Developer Documentation

StackOverflow https://stackoverflow.com/questions/9350319

  •  27-10-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

In Swift ...

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



// Later on..

func sliderDidMove(){
    print("The slider moved!")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top