NSToolbar 和视图控制器层次结构

发布于 2024-12-27 08:54:12 字数 979 浏览 4 评论 0原文

我有以下类层次结构:

NSViewController
  MyGeneralViewController
    ViewControllerA
    ViewControllerB

MyGeneralViewController 包含 ViewControllerAViewControllerB 使用的一些通用代码。一段常见代码是创建 NSToolbar 并为 NSToolbarDelegate 实现必要的协议方法。

工具栏中的按钮之一是允许用户在单独的窗口中打开 NSTextView 的按钮(更多空间、用于处理关键字的附加功能等)。

其方法是:

- (IBAction) openTextWindow: (id) sender {

    [self setTextWindowController: [[TextWindowController alloc] init]];
    [[self textWindowController] showWindow];

}

此工具栏按钮应该仅对 ViewControllerA 控制的视图可用。我的推理是,如果我仅为 ViewControllerA 实现 openTextWindow 方法,则对于由 ViewControllerB 控制的视图,工具栏按钮将显示为灰色(不可用) >,因为运行时系统会沿着类层次结构向上查找为工具栏按钮定义的方法,但找不到它。

事实似乎并非如此。当我使用 MyGeneralViewController 实现该方法时,该按钮始终可用(如预期但不是我想要的)。但是当我仅为 ViewControllerA 实现它时,它永远不可用。

我错过了什么还是这就是它应该工作的方式?

I have the following class hierarchy:

NSViewController
  MyGeneralViewController
    ViewControllerA
    ViewControllerB

MyGeneralViewController holds some common code used by both ViewControllerA and ViewControllerB. One piece of common code is the creation of an NSToolbar and the implementation of the necessary protocol methods for NSToolbarDelegate.

One of the buttons in the toolbar is a button that allows the user to open a NSTextView into a separate window (more real estate, additional functionality for working with keywords etc).

The method for this is:

- (IBAction) openTextWindow: (id) sender {

    [self setTextWindowController: [[TextWindowController alloc] init]];
    [[self textWindowController] showWindow];

}

This toolbar button should only be available for the view controlled by ViewControllerA. My reasoning was that if I implement the openTextWindow method only for ViewControllerA, the toolbar button would be greyed (not available) for the view controlled by ViewControllerB, as the runtime system would go up the class hierarchy to look up the method that was defined for the toolbarbutton and not find it.

This appears to be not the case. When I implement the method with MyGeneralViewController, the button is always available (as expected but not what I want). But when I implement it only for ViewControllerA it's never available.

Am I missing something or is this the way it is supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2025-01-03 08:54:12

诡异的。离开了一段时间后,我决定再试一次。对项目进行清理后,一切都按预期进行。

Weird. After leaving this for a while, I decided to give it another go. Following a clean of the project it all worked as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文