简体   繁体   English

NSDocument 的菜单栏没有出现

[英]Menu bar for NSDocument doesn't appear

I transplanted a storyboard to another project that uses xibs (yes, the deployment target for the app is 10.9).我将故事板移植到另一个使用 xibs 的项目中(是的,该应用程序的部署目标是 10.9)。 This storyboard is connected to a NSDocument subclass (available only on 10.10+) which seems to work very good as expected... but the only problem is the main menu that only appear when the window's document goes behind other windows (such Finder ones) and then I put it back in front.这个故事板连接到一个 NSDocument 子类(仅在 10.10+ 上可用),它似乎按预期工作得非常好......但唯一的问题是主菜单仅在窗口的文档落后于其他窗口(例如 Finder 的)时出现然后我把它放回前面。 My question is: how can I ensure the main menu get connected to my document?我的问题是:如何确保主菜单连接到我的文档?

  override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
    Swift.print("validateMenuItem")
    return super.validateMenuItem(menuItem)
  }

  override func makeWindowControllers() {
    let wc = DocumentWC.loadFromNib()

    self.addWindowController(wc!)
  }

Not sure what causing the problem (the project it's huge), and probably you can call this a patch instead of a fix:不确定是什么导致了问题(项目很大),也许您可​​以将其称为补丁而不是修复:

override func viewDidAppear() {
  super.viewDidAppear()
  if !self.menufixed {
    self.menufixed = true // just to call it once 
    let win = self.view.window
    win?.resignMain()
    win?.becomeMain()
    win?.orderFrontRegardless()
    win?.resignKey()
    win?.becomeKey()
    win?.orderFrontRegardless()
    NSApp.activate(ignoringOtherApps: true)
  }
}

PS added to the view controller when the view and the window appear.当视图和窗口出现时,PS 添加到视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM