[code]import Cocoa
class MainWindowController: NSWindowController {
var sliderOldValue: Double?
var sliderDefaultTickMarks: Int?
var sliderDefaultValue: Double?
@IBOutlet weak var slider: NSSlider!
@IBOutlet weak var sliderLabel: NSTextField!
@IBOutlet weak var radioShowTicks: NSButton!
@IBOutlet weak var radioHideTicks: NSButton!
@IBOutlet weak var checkbox: NSButton!
@IBOutlet weak var secretMessage: NSTextField!
@IBOutlet weak var decodedMessage: NSTextField!
override var windowNibName: String? {
return "MainWindowController"
}
override func windowDidLoad() {
super.windowDidLoad()
// get the number of tick marks defined in the xib
sliderDefaultTickMarks = slider.numberOfTickMarks
sliderDefaultValue = slider.doubleValue
// set everything up
initialiseControls()
}
@IBAction func resetButton(sender: NSButton) {
initialiseControls()
}
func initialiseControls() {
sliderOldValue = sliderDefaultValue!
slider.doubleValue = sliderOldValue!
adjustSlider(slider)
slider.numberOfTickMarks = sliderDefaultTickMarks!
radioShowTicks.state = NSOnState
secretMessage.stringValue = ""
decodedMessage.stringValue = ""
checkbox.state = NSOffState
setCheckboxText(checkbox)
}
@IBAction func adjustSlider(sender: NSSlider) {
let sliderValue: Double = sender.doubleValue
if sliderValue < sliderOldValue {
sliderLabel.stringValue = "Slider goes down!"
} else if sliderValue > sliderOldValue {
sliderLabel.stringValue = "Slider goes up!"
} else {
sliderLabel.stringValue = ""
}
sliderOldValue = sliderValue
}
@IBAction func changeSliderTickMarks(sender: NSButton) {
let tag = sender.tag
if tag == 1 { // hide tick marks
slider.numberOfTickMarks = 0
} else if tag == 2 { // show tick marks
slider.numberOfTickMarks = sliderDefaultTickMarks!
}
}
@IBAction func setCheckboxText(sender: NSButton) {
let checked = sender.state
if (checked == NSOffState) {
sender.title = "Check me"
} else if (checked == NSOnState) {
sender.title = "Uncheck me"
}
}
@IBAction func revealSecretMessage(sender: NSButton) {
decodedMessage.stringValue = secretMessage.stringValue
}
}[/code]
I decided to show off by reading the number of tick marks from the xib rather than having it in two places