同时打开多个文档,每个文档具有不同的菜单项状态 [英] Multiple documents open at the same time, each with different menu item states

查看:131
本文介绍了同时打开多个文档,每个文档具有不同的菜单项状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Cocoa文档的应用程序中,使菜单栏中的菜单项状态与当前可见文档保持同步的最佳方法是什么?

In a Cocoa document-based application, what's the best way to keep the menu item states in the menu bar in sync with the currently visible document?

例如,假设您有一个带有标尺视图的文档,可以通过显示标尺"(关闭状态)和隐藏标尺"(打开状态)菜单项隐藏/显示.用户同时打开两个文档.他/她已将标尺隐藏在一个文档中,但未隐藏在另一文档中.当用户在文档之间移动时,菜单项应反映该文档的状态,而不是整个应用程序的状态.

For example, say you have a document with a ruler view that can be hidden/shown via "Show Rulers" (off state) and "Hide Rulers" (on state) menu items. The user has two documents open at the same time. He/she has hidden the rulers in one document, but not on the other. When the user moves between the documents the menu item should reflect the state for that document, not for the application as a whole.

如何做到这一点?我已经四处搜寻,但似乎没有预定义的开箱即用"方法来执行此操作.如果NSDocument有某种"didRegainFocus"类型的方法可以在其中添加这种逻辑,那就太好了.

How does one do this cleanly? I've googled around but it doesn't seem like there's a predefined "out of the box" way to do this. It would be nice if NSDocument had some sort of "didRegainFocus" type method where such logic can be added.

我的实际实现有点棘手,因为它实际上是NSDocument内部的NSViewController,用于处理这些特定菜单项.

My actual implementation is a bit trickier since it's actually a NSViewController inside my NSDocument that deals with these particular menu items.

@interface MyDocument : NSDocument {
  SomeViewController *myCustomizedTextViewController;
}

MyDocument是当前活动文档时,我需要myCustomizedTextViewController中的方法来确保应用程序主菜单中几个NSMenuItem的状态.一个人是真正的统治者.另一个是类似的临时设置.

When MyDocument is the currently active document, I need methods in myCustomizedTextViewController to ensure the state of a couple of NSMenuItems in the application's main menu. One is genuinely a ruler. The other is a similar temporary setting.

推荐答案

使菜单项的目标实现validateUserInterfaceItem:方法来启用/禁用它们.有关更多信息,请参见用户界面验证参考.信息.

Have the target of the menu items implement the validateUserInterfaceItem: method to enable/disable them. See the User Interface Validation reference for more info.

这篇关于同时打开多个文档,每个文档具有不同的菜单项状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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