0

如何使用绑定显示时区标识符(使用结构)?例如,而不是 Africa/xxx 偏移 3600 我只想要 Africa/xxx

class TimezonePopupButton: NSPopUpButton {

    private var timezonesController: NSArrayController = NSArrayController()

    private var timeZones : [TimeZone] = {
        var content : [TimeZone] = []
        for identifier in TimeZone.knownTimeZoneIdentifiers {
            if let timeZone = TimeZone(identifier: identifier) {
                content.append(timeZone)
            }
        }
        return content
    }()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() {
        timezonesController.content = timeZones
        bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
        selectItem(withTitle: TimeZone.current.identifier)
    }
}

在此处输入图像描述

PS:可以用这个黑客来完成,但它看起来很不迅速,我担心它会在未来打破

extension NSTimeZone {

    open override func value(forKey key: String) -> Any? {
        if key == "description" {
            return self.name
        }
        return super.value(forKey: key)
    }
}
4

1 回答 1

1

绑定contentValues

在 NSPopUpButton 中显示为项目的字符串数组。

bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
bind(.contentValues, to: timezonesController, withKeyPath: "arrangedObjects.name")

请参阅NSPopUpButton 绑定

于 2020-05-18T11:20:27.890 回答