[code]class MainWindowController: NSWindowController {
enum TicksButton {
case showTicksBtn
case hideTicksBtn
init?(tagValue: Int) {
switch tagValue {
case 0: self = .showTicksBtn
case 1: self = .hideTicksBtn
default: return nil
} // end switch
}
}
// MARK: - Properties
let noTickMarks = 0
let allTickMarks = 10
var sliderValue = 0.0
override var windowNibName: String? {
return "MainWindowController"
}
// MARK: - IBOutlets
// from left to right
@IBOutlet weak var vSlider: NSSlider!
@IBOutlet weak var sliderStatus: NSTextField!
@IBOutlet weak var showTicks: NSButton!
@IBOutlet weak var hideTicks: NSButton!
@IBOutlet weak var checkBox: NSButton!
@IBOutlet weak var secretText: NSTextField!
@IBOutlet weak var plainText: NSTextField!
// MARK: - IBActions
// in a roughly left to right order
@IBAction func resetControls(sender: NSButton) {
// Sender: the "Reset Controls" button
// Action: call method to reset controls to initial states
resetAllControls()
}
@IBAction func adjustSlider(sender: NSSlider) {
// Sender: the vertical slider
// Action: give message as to direction of change, with color (bonus!)
let newValue = sender.doubleValue
if newValue == sliderValue {
sliderStatus.stringValue = "Slider stays same"
sliderStatus.textColor = NSColor.blackColor()
} else if newValue < sliderValue {
sliderStatus.stringValue = "Slider goes down!"
sliderStatus.textColor = NSColor.redColor()
} else {
sliderStatus.stringValue = "Slider goes up!"
sliderStatus.textColor = NSColor.blueColor()
}
sliderValue = newValue
}
@IBAction func toggleTicks(sender: NSButton) {
// Sender: the hide/show tick marks radio buttons
// Action: hide or show the tick marks as indicated
let ticksBtn = TicksButton(tagValue: sender.tag)
if let validTicksBtn = ticksBtn {
switch validTicksBtn {
case .showTicksBtn: vSlider.numberOfTickMarks = allTickMarks
case .hideTicksBtn: vSlider.numberOfTickMarks = noTickMarks
} // end switch
} // end if let
}
@IBAction func toggleCheckBox(sender: NSButton) {
// Sender: the checkbox button
// Action: toggle title of checkbox button
checkBox.title = (sender.state == NSOnState) ? "Uncheck me" : "Check me"
}
@IBAction func revealSecretText(sender: NSButton) {
// Sender: the "Reveal Secret Text" button
// Action: show the secret text as plain text
plainText.stringValue = secretText.stringValue
}
// MARK: --- Methods
override func windowDidLoad() {
super.windowDidLoad()
resetAllControls()
}
func resetAllControls() {
// Purpose: reset all controls to initial states
sliderValue = 0.0
vSlider.numberOfTickMarks = allTickMarks
vSlider.doubleValue = sliderValue
showTicks.state = NSOnState
sliderStatus.stringValue = ""
sliderStatus.textColor = NSColor.blackColor()
checkBox.title = "Check me"
checkBox.state = NSOffState
secretText.stringValue = ""
plainText.stringValue = ""
}
}
[/code]
My solution, for public consumption as it were. I may have over-engineered it slightly, especially in the toggleTicks func, but it works well.