Swift Playground中的NSPopUp按钮变灰

pzfprimi  于 2023-03-07  发布在  Swift
关注(0)|答案(2)|浏览(130)

在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应用程序中时,它就像正常一样工作,更奇怪的是,有一次我在截图时,它只工作了一次,然后又回到了灰色状态。
如有任何协助,不胜感激。

yyyllmsg

yyyllmsg1#

所以这个问题的解决方案有点奇怪。它只需要稍后定义目标。
也就是说

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

作品

23c0lvtd

23c0lvtd2#

同样的问题在这里,但在一个NSViewController的Objective-C项目中。

它与First Responder连锁店有关。

我可以用这个电话解决这个问题:

- (void)viewDidAppear
{
    [super viewDidAppear];

    [self.view.window makeFirstResponder: self];
}

相关问题