Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value of an NSSlider continuously?

It seems like NSSlider in Cocoa does not provide a delegate to receive an event like Value Changed for a UISlider.

How can I get the value of an NSSlider continuously and display it in an NSTextField, for example?

like image 472
meddlesome Avatar asked Jul 01 '11 21:07

meddlesome


3 Answers

You need to research Cocoa's Target/Action mechanism. This is a basic Cocoa concept you'll need to understand. The slider (and any other control) can be given a target (some controller object) and an action (the method to call against that controller object).

The action is fired when the user stops dragging by default. Check the slider's Continuous property in Interface Builder to cause it to trigger the action as you're sliding it.

like image 173
Joshua Nozzi Avatar answered Dec 21 '22 06:12

Joshua Nozzi


One advantage of using the timer approach is that it works for the case of using the keyboard rather than the mouse to adjust the slider. If the user has "Full Keyboard Access" turned on in System Preferences, they can use the Tab key to give the slider focus. They can then hold down an arrow key so that autorepeat kicks in, whereupon you have a similar situation to dragging with the mouse: the target/action is firing repeatedly, and you want to wait for a moment of calm before saving to the database.

You do need to be careful not to delete your NSTimer prematurely. For example, if the user quits the app during those couple of seconds you probably want to "flush" the slider value to the database before terminating the process.

like image 24
Andy Lee Avatar answered Dec 21 '22 05:12

Andy Lee


Programmatical solution based on the answer of Joshua Nozzi:

Swift

slider.isContinuous = true

Objective-C

slider.continuous = YES;
like image 38
Tamás Sengel Avatar answered Dec 21 '22 05:12

Tamás Sengel