How can I get the value of an NSSlider continuously?

只谈情不闲聊 提交于 2019-12-06 16:44:42

问题


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?


回答1:


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.




回答2:


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.




回答3:


Programmatical solution based on the answer of Joshua Nozzi:

Swift

slider.isContinuous = true

Objective-C

slider.continuous = YES;


来源:https://stackoverflow.com/questions/6553265/how-can-i-get-the-value-of-an-nsslider-continuously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!