Hi,
I am wondering if there is any solution for the challenge part.
Thanks!
Hi,
I am wondering if there is any solution for the challenge part.
Thanks!
Here’s mine:
MainWindowController.swift:
[code]import Cocoa
class MainWindowController: NSWindowController {
let numberOfTicksForRadioButton: [Int:Int] = [
0: 10, //A dictionary where the keys are the radio buttons' tags, and the values
1: 0, //are the number of tick marks associated with each radio button.
]
@IBOutlet weak var slider: NSSlider? //In the Attributes Inspector, in the Tick Marks section, I set the number to 10
@IBOutlet weak var radioButton0: NSButton? //In the Atttributes Inspector, I set the 'tag' to 0
//For the second radio button, I set the 'tag' to 1
@IBOutlet weak var checkBox: NSButton?
@IBOutlet weak var passwordTextField: NSTextField?
@IBOutlet weak var textField: NSTextField?
override var windowNibName: String? {
return "MainWindow"
}
@IBAction func onclickRadio(sender: NSButton) {
println("radio button number: \(sender.tag)")
if let validSlider = slider {
if let ticks = numberOfTicksForRadioButton[sender.tag] {
validSlider.numberOfTickMarks = ticks
}
else {
println("Non-existent key in numberOfTicksForRadioButton")
}
}
else {
println("Slider outlet hasn't been connected")
}
}
@IBAction func onclickCheckBox(sender: NSButton) {
sender.title = (sender.state == NSOnState) ? "Uncheck me" : "Check me"
}
@IBAction func onclickRevealButton(sender: NSButton) {
if let validPasswordTextField = passwordTextField,
validTextField = textField {
validTextField.stringValue = validPasswordTextField.stringValue
}
else {
println("Either the passwordField or the textFiled wasn't connected.")
}
}
}
@IBAction func onclickResetButton(sender: NSButton) {
if let validRadio0 = radioButton0 {
validRadio0.state = NSOnState
onclickRadio(validRadio0)
}
else {
println("radioButton0 wasn't connected")
}
if let validCheckBox = checkBox {
validCheckBox.state = NSOnState
onclickCheckBox(validCheckBox)
}
else {
println("The checkBox wasn't connected.")
}
if let validPasswordField = passwordTextField,
validTextField = textField {
validPasswordField.stringValue = ""
validTextField.stringValue = ""
}
else {
println("Either the passwordField or the textField wasn't connected.")
}
}
}
[/code]
One thing that surprised me: when I enable one radio button the other radio button is automatically disabled. I don’t understand how the two radio buttons know they are part of the same “group”.
Hah! It must be because they are connected to the same action. In other words, for all the radio buttons that have the same target+action, only one of the radio buttons can be enabled, so if you set one radio button to enabled, all the rest of the radio buttons with the same target+action automatically will be disabled. Yep, I tested it. You can create two groups of radio buttons by giving each group a different target+action. If you are using the same controller for both groups, e.g. MainWindowController, then you just need to connect all the radio buttons in one group to one @IBAction func, then connect all the radio buttons in the other group to another @IBAction func.