将可可粒放入可可中

发布于 2024-10-14 21:05:24 字数 477 浏览 2 评论 0原文

如何将笔尖加载到另一个窗口内?

我尝试了 initWithWindowName,

if (mmController == NULL)
    mmController = [[mainMenu alloc] initWithWindowNibName:@"mainMenu"];
[mmController showWindow:self];

但它打开了一个新窗口。

我也尝试了 loadNibNamed

[NSBundle loadNibNamed:@"mainGame" owner:self];

并且成功了,但是当我尝试使用相同的方法返回主菜单时,

[NSBundle loadNibNamed:@"mainMenu" owner:self];

它不起作用。它根本没有任何作用... 有什么想法吗?

How can I load a nib inside of another window?

I tried initWithWindowName,

if (mmController == NULL)
    mmController = [[mainMenu alloc] initWithWindowNibName:@"mainMenu"];
[mmController showWindow:self];

but it opens a new window.

I also tried loadNibNamed

[NSBundle loadNibNamed:@"mainGame" owner:self];

and it succeeded, but when I try to use the same method to get back to the main menu,

[NSBundle loadNibNamed:@"mainMenu" owner:self];

it doesn't work. It does nothing at all...
Any ideas?

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

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

发布评论

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

评论(1

决绝 2024-10-21 21:05:24

我尝试了 initWithWindowName,

您的意思是 initWithWindow¹Nib²Name³:,它采用包含窗口 (1) 的笔尖 (2) 的名称 (3)。

if (mmController == NULL)

这应该是 nil,而不是 NULL,因为您正在比较 Objective-C 对象指针。

 mmController = [[mainMenu alloc] initWithWindowNibName:@"mainMenu"];

这里的 mainMenu 是什么?它一定是一个类,但它是什么的子类呢?

[mmController showWindow:self];

从这条消息和上一条消息来看,我猜测 mainMenu 是 NSWindowController 的子类。

不应该需要猜测。您应该专门命名您的类,以便任何人都可以仅通过类名就知道该类是什么及其实例。

简洁是一种美德,但如果你需要长篇大论,那就长篇大论。我们拥有具有名称补全功能的现代工具。 Tab 键可以消除缩写名称的唯一优势。

但它会打开一个新窗口。

是的。您通过从笔尖加载窗口来创建窗口,然后告诉窗口控制器显示该窗口。显示一个新窗口是预期的结果。

我也尝试过loadNibNamed

[NSBundle loadNibNamed:@"mainGame" 所有者:self];

它成功了,但是当我尝试使用相同的方法返回主菜单时,

没有“返回”。加载笔尖只是通过从存档加载对象来创建对象。您可以多次加载同一个笔尖,并且加载笔尖不会以某种方式撤消加载前一个笔尖的结果。

您可能需要阅读资源编程指南,其中涵盖了笔尖和图像和声音文件,以及捆绑包编程指南

如果您想隐藏从 mainGame nib 加载的窗口,请执行此操作。 AppKit 中的术语是 “订购出”(与“订购入”相反,“订购前端”“退回订单” 是具体的做法)。

[NSBundle loadNibNamed:@"mainMenu" 所有者:self];

这不起作用。它根本没有任何作用...

您是否正在尝试加载项目附带的 MainMenu 笔尖?如果是这样,请确保您的大小写正确 - 您不希望您的应用程序因从区分大小写的卷运行它的人而被破坏,也不希望它被使用默认的不区分大小写的卷的人破坏。文件系统。

如果这不是您想要做的,那么就不清楚您想要做什么。 MainMenu 通常是包含主菜单(菜单栏的内容)的笔尖;将任何其他笔尖命名为“mainMenu”或类似名称,往好里说会引起混乱,往坏里说会出现问题。如果这是其他笔尖,您应该给它一个不同的名称。

无论哪种方式,这都不是您需要做的。如果您想隐藏从 mainGame 加载的窗口,那么您需要隐藏该窗口,而不是加载不同的笔尖。

此外,一旦窗口被加载,就不要再次加载它(除非您关闭并释放它)。加载后,您只需将其重新订购即可。最有可能的是,您会想要 将其设置为键并排序在前面

在 Mac 上,您一次不仅限于一个窗口;事实上,无论您做什么,您的应用程序都有多个窗口(至少三个)。 API 是围绕您显示多个窗口的能力而构建的。

有关详细信息,请参阅窗口编程指南

如何在另一个窗口中加载笔尖?

正如 Justin Meiners 已经告诉您的那样,您可能需要 NSViewController,尽管您可以不使用 NSViewController,而直接使用 loadNibNamed: 加载包含视图的笔尖。

请注意,NSViewController 并不像 Cocoa Touch 的 UIViewController 那样强大/有特色。

为此,您需要阅读查看编程指南

I tried initWithWindowName,

You mean initWithWindow¹Nib²Name³:, which takes the name (3) of a nib (2) containing a window (1).

if (mmController == NULL)

This should be nil, not NULL, since you are comparing an Objective-C object pointer.

    mmController = [[mainMenu alloc] initWithWindowNibName:@"mainMenu"];

What is mainMenu here? It must be a class, but what is it a subclass of?

[mmController showWindow:self];

From this message and the previous message, I'm guessing mainMenu is a subclass of NSWindowController.

Guessing should not be required. You should name your classes specifically, so that anybody can tell what the class is and its instances are merely by the class name.

Brevity is a virtue, but if you need to go long, go long. We've got modern tools with name completion. The tab key can eliminate the sole advantage of an abbreviated name.

but it opens a new window.

Yes. You created a window by loading it from a nib, and then you told the window controller to show that window. Showing a new window is the expected result.

I also tried loadNibNamed

[NSBundle loadNibNamed:@"mainGame" owner:self];

and it succeeded, but when I try to use the same method to get back to the main menu,

There is no “get back”. Loading a nib is simply creating objects by loading them from an archive. You can load the same nib multiple times, and loading a nib does not somehow undo the results of loading a previous nib.

You may want to read the Resource Programming Guide, which covers nibs as well as image and sound files, and the Bundle Programming Guide.

If you want to hide the window you loaded from the mainGame nib, do that. The term for this in AppKit is “ordering out” (as opposed to “ordering in”, which “ordering front” and “ordering back” are specific ways of doing).

[NSBundle loadNibNamed:@"mainMenu" owner:self];

it doesn't work. It does nothing at all...

Are you trying to load the MainMenu nib that came with your project? If so, make sure you get the case right—you don't want your app to be broken for people who run it from a case-sensitive volume, nor do you want it to be broken for people who use the default case-insensitive file-system.

If that's not what you're trying to do, then it isn't clear what you are trying to do. MainMenu is normally the nib containing the main menu (the contents of the menu bar); naming any other nib “mainMenu” or anything like that is going to cause confusion at best and problems at worst. If this is supposed to be some other nib, you should give it a different name.

Either way, this is not what you need to do. If you want to hide the window you loaded from mainGame, then you need to hide that window, not load a different nib.

Moreover, once the window is loaded, do not load it again (unless you close and release it). Once you have loaded it, you can simply order it back in. Most probably, you will want to both make it key and order it front.

On the Mac, you are not limited to one window at a time; indeed, your app has multiple windows (at least three), no matter what you do. The APIs are built around your ability to show multiple windows.

See the Window Programming Guide for more information.

How can I load a nib inside of another window?

As Justin Meiners already told you, you may want NSViewController for that, although you can go without and just load the nib containing the view directly using loadNibNamed:.

Be warned that NSViewController is not nearly as powerful/featureful as Cocoa Touch's UIViewController.

You'll want to read the View Programming Guide for this.

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