使用绑定和结构时自定义 nspopupbutton 标题
Custom nspopupbutton title when using bindings and struct
如何显示带绑定的时区标识符(使用结构)?例如而不是 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: 可以用这个 hack 来完成,但它看起来很不敏捷,我担心它会在未来崩溃
extension NSTimeZone {
open override func value(forKey key: String) -> Any? {
if key == "description" {
return self.name
}
return super.value(forKey: key)
}
}
绑定contentValues
An array of strings that are displayed as the items in the NSPopUpButton.
bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
bind(.contentValues, to: timezonesController, withKeyPath: "arrangedObjects.name")
如何显示带绑定的时区标识符(使用结构)?例如而不是 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: 可以用这个 hack 来完成,但它看起来很不敏捷,我担心它会在未来崩溃
extension NSTimeZone {
open override func value(forKey key: String) -> Any? {
if key == "description" {
return self.name
}
return super.value(forKey: key)
}
}
绑定contentValues
An array of strings that are displayed as the items in the NSPopUpButton.
bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
bind(.contentValues, to: timezonesController, withKeyPath: "arrangedObjects.name")