简体   繁体   中英

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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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