在Swift操场视图控制器中,我有以下代码:
var jsonSelector = NSPopUpButton(title: "Path", target: self, action: #selector(updatePointFile))
override public func loadView() {
let frame = CGRect(origin: CGPoint.zero, size: CGSize(width: 900, height: 600))
let view = NSView(frame: frame)
let array = // gets array of items
for item in array {
jsonSelector.addItem(withTitle: item)
}
view.addSubview(jsonSelector)
self.view = view
}
@objc func updatePointFile() {
let file = jsonSelector.selectedItem?.title ?? "swiftLogo"
...
}
运行时,最初看起来很正常:
但是,一旦单击它,它看起来像:
当您单击鼠标左键时,它将保持不可点击状态:
当我将完全相同的代码复制到完整的Mac应用程序中时,它的工作原理与正常一样,并且使事情变得更加奇怪,有一次,当我拍摄这些屏幕截图时,它可以一次选择并返回到灰色状态。
任何帮助将不胜感激。
最佳答案
因此,解决此问题的方法有点怪异。它只需要稍后定义目标即可。
这意味着
var jsonSelector = NSPopUpButton(frame: NSRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 50)))
override public func loadView() {
jsonSelector.target = self
jsonSelector.action = #selector(updatePointFile)
let array = // gets array of items
for item in array {
jsonSelector.addItem(withTitle: item)
}
view.addSubview(jsonSelector)
self.view = view
}
@objc func updatePointFile() {
let file = jsonSelector.selectedItem?.title ?? "swiftLogo"
...
}
要么
var jsonSelector = NSPopUpButton(title: "Path", target: self, action: #selector(updatePointFile))
override public func loadView() {
let frame = CGRect(origin: CGPoint.zero, size: CGSize(width: 900, height: 600))
let view = NSView(frame: frame)
jsonSelector.target = self
let array = // gets array of items
for item in array {
jsonSelector.addItem(withTitle: item)
}
view.addSubview(jsonSelector)
self.view = view
}
@objc func updatePointFile() {
let file = jsonSelector.selectedItem?.title ?? "swiftLogo"
...
}
作品