从单独的源文件关闭一个源文件中的面板

发布于 2024-09-09 04:26:17 字数 313 浏览 1 评论 0原文

这是我到目前为止所得到的。

- (IBAction)HUDPanelDictionaryHide:(id)sender{
    extern NSPanel HUDPanelDictionary;
     [HUDPanelDictionary close];
}

这显然行不通。

HUDPanelDictionary 在 .xib 文件的单独 .h 和 .m 文件中声明。我需要从另一个 .xib 文件的其他 .h 和 .m 文件中关闭此面板。抱歉我说得这么含糊!

有什么想法吗?

以利亚

This is what I got so far.

- (IBAction)HUDPanelDictionaryHide:(id)sender{
    extern NSPanel HUDPanelDictionary;
     [HUDPanelDictionary close];
}

This obviously does NOT work.

The HUDPanelDictionary is declared in a separate .h and .m file for an .xib file. I need to close this panel from the other .h and .m file for another .xib file. Sorry I'm being so vague!

Any ideas??

Elijah

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

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

发布评论

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

评论(1

我为君王 2024-09-16 04:26:17

您需要#import包含 HUDPanelDictionary 声明的标头。

例如:

#import "HUDPanelDictionary.h"

@interface MyController

- (IBAction)hideDictionaryPanel:(id)sender {
    [HUDPanelDictionary close];
}

@end

我也会以不同的方式命名事物,例如“DictionaryHUD”而不是“HUDPanelDictionary”。 “Panel”与“HUD”是多余的,您应该更关心它的意图而不是它在类层次结构中的位置。

我要做的另一件事是使 DictionaryHUD 成为 NSWindowController 子类,并让它公开一个单例共享实例,而不是使用全局变量指向面板本身。那么上面的代码将如下所示:

#import "DictionaryController.h"

@interface MyController

- (IBAction)hideDictionaryPanel:(id)sender {
    [[DictionaryController sharedDictionaryController] hideDictionaryPanel:sender];
}

@end

这将字典面板/HUD 的主要职责放在单个控制器类的实例上,其他控制器(例如管理主窗口工具栏的控制器)可以将其交互转发到该实例。您甚至可以将字典 HUD 窗口控制器放入响应程序链中,使其自动处理诸如 -hideDictionaryPanel: 之类的操作,因此无需执行此类转发。

You need to #import the header containing the declaration of HUDPanelDictionary.

For example:

#import "HUDPanelDictionary.h"

@interface MyController

- (IBAction)hideDictionaryPanel:(id)sender {
    [HUDPanelDictionary close];
}

@end

I would also name things differently, for example "DictionaryHUD" rather than "HUDPanelDictionary." "Panel" is redundant with "HUD," and you should care more about its intent than its position in the class hierarchy.

Another thing I would do is make DictionaryHUD an NSWindowController subclass, and have it expose a singleton shared instance rather than use a global variable to point to the panel itself. Then the code above would look like this:

#import "DictionaryController.h"

@interface MyController

- (IBAction)hideDictionaryPanel:(id)sender {
    [[DictionaryController sharedDictionaryController] hideDictionaryPanel:sender];
}

@end

This puts the primary responsibility for your dictionary panel/HUD on an instance of a single controller class, to which other controllers (say one that manages your main window's toolbar) can forward their interactions. You could even put the dictionary HUD window controller in the responder chain to have it automatically handle actions like -hideDictionaryPanel: so nothing needs to do that kind of forwarding.

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