将可可粒放入可可中
如何将笔尖加载到另一个窗口内?
我尝试了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是
initWithWindow¹Nib²Name³:
,它采用包含窗口 (1) 的笔尖 (2) 的名称 (3)。这应该是
nil
,而不是NULL
,因为您正在比较 Objective-C 对象指针。这里的
mainMenu
是什么?它一定是一个类,但它是什么的子类呢?从这条消息和上一条消息来看,我猜测 mainMenu 是 NSWindowController 的子类。
不应该需要猜测。您应该专门命名您的类,以便任何人都可以仅通过类名就知道该类是什么及其实例。
简洁是一种美德,但如果你需要长篇大论,那就长篇大论。我们拥有具有名称补全功能的现代工具。 Tab 键可以消除缩写名称的唯一优势。
是的。您通过从笔尖加载窗口来创建窗口,然后告诉窗口控制器显示该窗口。显示一个新窗口是预期的结果。
没有“返回”。加载笔尖只是通过从存档加载对象来创建对象。您可以多次加载同一个笔尖,并且加载笔尖不会以某种方式撤消加载前一个笔尖的结果。
您可能需要阅读资源编程指南,其中涵盖了笔尖和图像和声音文件,以及捆绑包编程指南。
如果您想隐藏从
mainGame
nib 加载的窗口,请执行此操作。 AppKit 中的术语是 “订购出”(与“订购入”相反,“订购前端” 和 “退回订单” 是具体的做法)。您是否正在尝试加载项目附带的 MainMenu 笔尖?如果是这样,请确保您的大小写正确 - 您不希望您的应用程序因从区分大小写的卷运行它的人而被破坏,也不希望它被使用默认的不区分大小写的卷的人破坏。文件系统。
如果这不是您想要做的,那么就不清楚您想要做什么。 MainMenu 通常是包含主菜单(菜单栏的内容)的笔尖;将任何其他笔尖命名为“mainMenu”或类似名称,往好里说会引起混乱,往坏里说会出现问题。如果这是其他笔尖,您应该给它一个不同的名称。
无论哪种方式,这都不是您需要做的。如果您想隐藏从
mainGame
加载的窗口,那么您需要隐藏该窗口,而不是加载不同的笔尖。此外,一旦窗口被加载,就不要再次加载它(除非您关闭并释放它)。加载后,您只需将其重新订购即可。最有可能的是,您会想要 将其设置为键并排序在前面。
在 Mac 上,您一次不仅限于一个窗口;事实上,无论您做什么,您的应用程序都有多个窗口(至少三个)。 API 是围绕您显示多个窗口的能力而构建的。
有关详细信息,请参阅窗口编程指南。
正如 Justin Meiners 已经告诉您的那样,您可能需要 NSViewController,尽管您可以不使用 NSViewController,而直接使用
loadNibNamed:
加载包含视图的笔尖。请注意,NSViewController 并不像 Cocoa Touch 的 UIViewController 那样强大/有特色。
为此,您需要阅读查看编程指南。
You mean
initWithWindow¹Nib²Name³:
, which takes the name (3) of a nib (2) containing a window (1).This should be
nil
, notNULL
, since you are comparing an Objective-C object pointer.What is
mainMenu
here? It must be a class, but what is it a subclass of?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.
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.
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).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.
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.