可可验证多个窗口中的菜单项 [英] Cocoa validate menu items in multiple windows

查看:325
本文介绍了可可验证多个窗口中的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单项 foobar,需要在主窗口(应用程序委托)上启用,但在所有其他窗口上禁用。我想知道该怎么做,因为第一响应者业务对我来说非常混乱。这就是我现在拥有的东西:

I have a menu item "foobar" that i need to enable on my main window (app delegate), but disable on all other windows. I'd like to know how to do that, because the first responder business is very confusing to me. Here's what i have now:


  • foobar项连接到MainMenu.xib中第一响应者的 foobar:自定义操作
  • >
  • 主应用程序委托中有一个 foobar:操作,因此菜单项已启用并起作用

  • 现在我加载并在另一个窗口中 makeKeyAndOrderFront

  • 我将一些控制权集中在新窗口上

  • 这是应禁用 foobar项的位置,但并非如此

  • "foobar" item is connected to first responder's "foobar:" custom action in MainMenu.xib
  • there is a "foobar:" action in the main app delegate so the menu item is enabled and works
  • now i load and "makeKeyAndOrderFront" another window
  • i focus some control on that new window
  • this is the place where my "foobar" item should be disabled, but it's not

我可以看到在应用程序委托中正在调用 validateMenuItem,但是在第二个窗口的控制器中并未对其进行调用。

I can see that the "validateMenuItem" is being called in the app delegate, but it's not being called in the second window's controller.

有人可以用非常简单的方式向我解释为什么会发生这种情况以及我必须解决什么选择吗?

Could someone explain it to me in very simple terms why this is happening and what options i have to solve this?

谢谢出于您的想法

推荐答案

将您的应用程序委托设置为主窗口的委托,并实现以下方法:

Set your app delegate as delegate of the main window, and implement these method:

- (void)windowDidBecomeKey:(NSNotification *)notification
{
    [foobar setEnabled: YES];
}

- (void)windowDidResignKey:(NSNotification *)notification
{
    [foobar setEnabled: NO];
}

关于第一响应者

第一个响应者是窗口内的NSControl,例如在特定窗口中,您有两个文本字段:textField1和textField2。键入时,输入仅到达第一个响应者(一个因此,您需要知道哪个窗口是关键,而不必知道在特定情况下谁是第一响应者。

The first responder is a NSControl that is inside the window.For example on a particular window you have two text fields: textField1 and textField2.When you type the input goes just to the first responder (one between all the controls that accept the first responder).So what you need is to know which window is key, you don't need to know who is the first responder in your specific case.

编辑

还有另一种方法(可能更快,但取决于个人喜好):通过界面生成器,请仅在某个窗口为键时选择要启用的菜单项。假设该窗口是名为window1的应用程序委托的ivar。然后单击该菜单项,转到绑定检查器,在启用下选择绑定到:应用程序委托,模型密钥路径:self.window.isKeyWindow。

There is also another (maybe faster, but depends on personal preferences) way to do it: through interface builder, select the menu item that you want to make enabled only when a certain window is key.Let's suppose that this window is an ivar of the app delegate named window1.Then click on that menu item, go to the bindings inspector, under "enabled" select bind to: app delegate, model key path: self.window.isKeyWindow.

一些图像提示:

这篇关于可可验证多个窗口中的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆