Menus Not Loading From Nib

Moderator
Posts: 613
Joined: 2004.09
Post: #1
Im wrapping up my latest game and I just got around to the menu bar (that shows during windowed mode), I really dont use it since I have in game menus that handle everything. The problem is its there and it wont change.

The first issue is I have two of them, If I select quit from the first one the menu closes and goes to the other one. I assumed a long time ago that I could grab the event from the first quit and pass it to my games quit function.

The problem I am having is I seem to have no control over the menu, Im attempting to load it from my nib to change the menu title (since its still generic). I using the following code

Code:
    IBNibRef gNibs = nil;
CFBundleRef gBundle = nil;
OSStatus    iErr;
    
    gBundle = CFBundleGetMainBundle();
    if (gBundle == nil)
        DoFatalAlert("\pCannot locate Main Bundle!");
        
    iErr = CreateNibReferenceWithCFBundle(gBundle, CFSTR("Game"), &gNibs);
    if (iErr != noErr)
        DoFatalAlert("\pCreateNibReferenceWithCFBundle failed!");
        
    iErr = SetMenuBarFromNib(gNibs, CFSTR("NSMenu1"));
    if (iErr != noErr)
    {
        DoFatalAlert("\pSetMenuBarFromNib failed!");
    }

I get no errors up until the SetMenuBarFromNib, where it fails with a iErr = -10, I trippled checked the name of it and even attempted to change it. Is there a reason I am missing on why it wont load the Menu? Im kinda stumped on this because all the documentation I found says this should work fine.

Thanks for the help as always everyone. =)

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
dair
Unregistered
 
Post: #2
That code looks fine - my guess would be that you're either loading the wrong bundle, or you don't have the menu in the nib you're loading. Perhaps try using CFShow to dump out the bundle and nib, and double-check the menu name in the nib file is correct?

You can see similar code in /Developer/Examples/Carbon, so you may want to use that as a test case to see what's different.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #3
Everything checks out, tripled check the names on everything again, and I only have one nib file. I'll toy around with it some more, but if anyone else has any suggestions im all ears.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #4
I added in some error handlers, according to the documentation it should fail only because one of these three reasons, It dosnt return anything that matches them and I get an unknown error (-10). Just thought I would provide some more info incase anyone was thinking about it besides me.

Code:
gBundle = CFBundleGetMainBundle();
    if (gBundle == nil)
        DoFatalAlert("\pCannot locate Main Bundle!");
        
    iErr = CreateNibReferenceWithCFBundle(gBundle, CFSTR("Game"), &gNibs);
    if (iErr != noErr)
        DoFatalAlert("\pCreateNibReferenceWithCFBundle failed!");
        
    iErr = SetMenuBarFromNib(gNibs, CFSTR("MainMenu"));
    if (iErr == kIBCarbonRuntimeCantFindNibFile)
    {
        DoFatalAlert("\pkIBCarbonRuntimeCantFindNibFile!");
    }
    
    if (iErr == kIBCarbonRuntimeObjectNotOfRequestedType)
    {
        DoFatalAlert("\pkIBCarbonRuntimeObjectNotOfRequestedType!");
    }
        
    if (iErr == kIBCarbonRuntimeCantFindObject)
    {
        DoFatalAlert("\pkIBCarbonRuntimeCantFindObject!");
    }
    
    if (iErr != noErr)
    {
        printf("Error : %i\n", iErr);
        DoFatalAlert("\pFailed with an Unknown Error!");
    }

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
dair
Unregistered
 
Post: #5
Is it possible that you're not launching the copy of the app you think you are? I.e., if you insert a CFShow(gBundle) after CFBundleGetMainBundle, does that print the path you'd expect? And if you open up that app's package, and load its .nib into IB, the menu bar is present?

Your code is fine - if you create a new Carbon Application in Xcode, and paste it into main, then after changing "Game" to "main" and "MainMenu" to "MenuBar" then you'll see everything get loaded up as you expect.

So if the code is fine, the problem must be in the .nib - either the wrong one is being loaded, or the one you've got has a problem in it. CFShow/Show Package Contents should rule out the first, and for the second you might want to look at the objects.xib file inside your .nib package.

This is just a text XML file that defines the objects in the .nib, but will let you confirm it's not something like a stray space character at the end of the menu name.

If there's no obvious problems with it, I would try creating a new nib from scratch and confirm you can load a menu out of that - I suppose it's possible your existing .nib has been corrupted somehow, although you'd think if that was the case then IB would also refuse to load it.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #6
Amazingly that works but dosnt really slove my problem, I still cant change the name of the application through the menu bar. Although I can now catch the quit command.

Thanks Dair, it must of been corrupted somehow. I do have a second followup question is there a way to change the application name after I create a project or should I create a new project and import all my source files?

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
dair
Unregistered
 
Post: #7
The string displayed at the top of the application menu is set by the CFBundleName value in your Info.plist (or localised equivalent).

I don't believe there is any supported way to change this at runtime, so you'll need to set an appropriate CFBundleName value before you are launched.
Quote this message in a reply
Post Reply