user3750385
user3750385

Reputation: 13

How do you use a Cocoa slider control in SWIFT with OSX

I create an IBAction using Interface Builder

  @IBAction func slider(sender : NSSlider) {
    var x:Double = Double(self.slider.value())

}

Now I would like to fetch and work with its current value. Does anyone have good suggestions for learning Cocoa development using SWIFT on OSX? In tutorials, I see references to UISlider and UIButton but Interface Builder creates references in code to NSButton and NSSlider. I'm confused. Thank you in advance.

Upvotes: 1

Views: 3535

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

All the "UI" prefixed controls are part of the UIKit framework, which is used for developing iOS apps. If you're developing for OS X, you'll be using AppKit, which came first and uses the "NS" prefix for its controls. There probably aren't many tutorials for Swift OS X development yet, but the Cocoa frameworks that you're working with are the same regardless of your choice of Swift vs. Objective-C.

NSSlider doesn't have a value() method, but it does have a doubleValue property, which is what you need. The method you've set up will be called when the slider's value changes:

@IBAction func slider(sender : NSSlider) {
    var x: Double = sender.doubleValue
    // do something
}

If you want to get the slider's value at some arbitrary time, you'll want to set up an IBOutlet instead:

@IBOutlet var slider: NSSlider

Upvotes: 4

Related Questions