load nibs in cocoa

孤街醉人 提交于 2020-01-11 06:35:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4843016/load-nibs-in-cocoa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!