Bind properties in Swift to NSTextFields

前端 未结 2 1633
一整个雨季
一整个雨季 2021-01-25 22:29

I\'m working on a unit converter written in Swift that will automatically display the updated units within the appropriate NSTextFields. For this example, if the us

相关标签:
2条回答
  • 2021-01-25 22:49

    You need to move the setting of the text fields' values out of viewDidLoad() to a separate method. Call that method from viewDidLoad() and also from a didSet property observer on your seconds property.

    You also need to create action methods for when the seconds, minutes, or hours fields are changed. In the action method for the seconds field, set the seconds property to secondsField.floatValue. Similarly for the other two fields. In the NIB/storyboard, connects the text fields' actions to the corresponding action method on the view controller.

    0 讨论(0)
  • 2021-01-25 23:06

    Is it possible to have a function that will update all the other text fields other than the currently active field?

    Yes. You can connect all text field to the same action.

    First connect all your text fields accordingly to your view controller:

    @IBOutlet weak var hourField: NSTextField!
    @IBOutlet weak var minuteField: NSTextField!
    @IBOutlet weak var secondField: NSTextField!
    

    Second create a single var to represent your time interval. You can also add a setter / getter to store it automatically to USerDefaults:

    var timeInterval: TimeInterval {
        get {
            return UserDefaults.standard.double(forKey: "timeInterval")
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "timeInterval")
        }
    }
    

    Third create a TimeInterval extension to convert the time from seconds to hour/minute

    extension TimeInterval {
        var second: Double { return self }
        var minute: Double { return self / 60 }
        var hour:   Double { return self / 3600 }
        var hourToMinute:   Double { return self * 60 }
        var hourToSecond:   Double { return self * 3600 }
        var minuteToHour:   Double { return self / 60 }
        var minuteToSecond: Double { return self * 60 }
    }
    

    Fourth create the action that will update the other text fields:

    @IBAction func timeAction(sender: NSTextField) {
        // use guard to convert the field string to Double
        guard let time = Double(sender.stringValue) else { return }
        // switch the field  
        switch sender {
        case hourField:
            // convert / update secondary fields
            minuteField.stringValue = time.hourToMinute.description
            secondField.stringValue = time.hourToSecond.description
            // make it persist through launches
            timeInterval = time * 3600
        case minuteField:
            hourField.stringValue = time.minuteToHour.description
            secondField.stringValue = time.minuteToSecond.description
            timeInterval = time * 60
        default:
            hourField.stringValue = time.hour.description
            minuteField.stringValue = time.minute.description
            timeInterval = time
        }
    }
    

    Last but not least make sure you load the values next time your view will appear:

    override func viewWillAppear() {
        hourField.stringValue = timeInterval.hour.description
        minuteField.stringValue = timeInterval.minute.description
        secondField.stringValue = timeInterval.second.description
    }
    

    Sample Project

    0 讨论(0)
提交回复
热议问题