在Swift playground视图控制器中,我有以下代码:
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"
...
}
作品