NSPopUpButton在Swift Playground中变灰

时间:2019-02-23 17:59:05

标签: swift macos swift-playground appkit

在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"
    ...
}

它运行时最初看起来很正常:
normal

但是一旦单击它,它看起来就像:
broken

当您单击鼠标左键时,它将保持不可点击状态:
still broken

当我将完全相同的代码复制到完整的Mac应用程序中时,它的工作原理就像正常的一样,甚至使事情变得更奇怪,有一次,当我拍摄这些屏幕截图时,它只能选择一次并返回到灰色状态。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

所以解决这个问题有点奇怪。只需稍后定义目标即可。

这意味着

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"
    ...
}

有效