【问题标题】:Swift - NSMenu Item Popover doesnt get FocusSwift - NSMenuItem Popover 没有获得焦点
【发布时间】:2015-07-30 14:23:08
【问题描述】:

所以我遇到了一个奇怪的问题,这有点难以解释,但我会尝试。

我有一个状态菜单项:

let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)

它包含一个 NSMenu :

        let menu = NSMenu()
        statusItem.menu = menu }

现在我向它添加自定义菜单项,它可以包含按钮或我想要的任何内容:

    var newController = storyBoard!.instantiateControllerWithIdentifier("testViewController") as! testCustomViewController
    var menueItem = NSMenuItem(title: title, action: action, keyEquivalent: key)
    menueItem.view = newController.view
    statusItem.menu?.addItem(menueItem)

之后,我将一个 testButton 放入菜单中,然后让他调用一个 Action。

    @IBAction func testClick(sender: NSButton) {
    appDelegate.testPopover(sender)
}

到目前为止,一切都很好,并且作为意向,我可以与一切互动,一切都很好而且很酷。 我可以点击图标,菜单打开并显示我想要的任何内容。

但是现在问题来了。 如果我在按钮上打开一个弹出框(效果很好)并且弹出框打开,我无法与弹出框交互。 我仍然可以与菜单和按钮交互,一切都很酷。 但只要我点击弹出框,一切都会隐藏起来。 (它不会崩溃什么的,它只是关闭弹出框、菜单等)

我这样打开 Popover。 在初始化中:

let Popover = NSPopover()
Popover.contentViewController = storyBoard!.instantiateControllerWithIdentifier("test") as! NSViewController

在 testPopoverMethod 中:

Popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSMinYEdge)

所以我尝试了很多东西,比如“resignFirstResponder”、“becomeFirstResponder”和 ofc 我试图改变行为,但我无法解决这个问题。

这是截图 http://imgur.com/Wj95eQw 如果我单击 Popover 中的任何位置,所有东西都会隐藏,不管在哪里,如果有一个按钮或一个文本字段,没有区别......

我该如何解决这个问题?

【问题讨论】:

  • 我相信我知道问题出在哪里。我的 NSMenu 确实将 Popover 中的点击计为“外部”点击,因此菜单关闭,Popover 也随之消失。但是我怎样才能让菜单保持打开状态?
  • 没人能帮忙吗? :-/

标签: macos swift nsmenu nsviewcontroller nspopover


【解决方案1】:

试试

func testPopover(sender: AnyObject?) {
        NSRunningApplication.currentApplication().activateWithOptions(NSApplicationActivationOptions.ActivateIgnoringOtherApps)
        Popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSMinYEdge)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多