简体   繁体   English

打开窗口后无法选择NSMenuItem

[英]NSMenuItem's not selectable after opening window

I'm having an issue very similar to this thread . 我遇到的问题与此线程非常相似。

I am programatically creating a NSMenu and adding my items. 我正在以编程方式创建NSMenu并添加我的项目。 One selection of an item it shows a window. 一项的选择将显示一个窗口。 This works as intended. 这按预期工作。 However, when I close the window I can no longer select any of the options in the menu. 但是,当我关闭窗口时,无法再选择菜单中的任何选项。

AppDelegate.m AppDelegate.m

- (void)createMenu {
      NSMenu *statusMenu = [[NSMenu alloc] initWithTitle:@""];

      NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:@"History" action:@selector(onHistory:) keyEquivalent:@""];

      [statusMenu addItem:historyItem];

      NSImage *statusImage = [NSImage imageNamed:@"icon.png"];
      [_item setImage:statusImage];
      [_item setMenu:statusMenu];
}

- (void)onHistory:(id)sender {
      OBHistoryWindowController *historyWindowController = [[OBHistoryWindowController alloc] initWithWindowNibName:@"OBHistoryWindowController"];
      historyWindowController.managedContext = self.managedObjectContext;
      [historyWindowController showWindow];
}

OBHistoryWindowController.m OBHistoryWindowController.m

- (void)showWindow {
      [NSApp runModalForWindow:self.window];
}

I'm guessing I need to somehow on close of the window give focus back to the menu but I can't for the life of me figure out how. 我猜想我需要以某种方式关闭窗口以将焦点返回到菜单,但是我无法终生想出如何做。

It sounds like you haven't stopped the modal loop. 听起来您还没有停止模式循环。 As the docs for runModalForWindow: say, "You can exit the modal loop by calling the stopModal , stopModalWithCode: , or abortModal methods from your modal window code." 正如runModalForWindow:的文档所述runModalForWindow: “您可以通过从模态窗口代码中调用stopModalstopModalWithCode:abortModal方法来退出模态循环。”

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

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