Re: how to load an NSMenu out of a nib?
Re: how to load an NSMenu out of a nib?
- Subject: Re: how to load an NSMenu out of a nib?
- From: Stuart Malin <email@hidden>
- Date: Wed, 8 Apr 2009 14:34:10 -1000
On Apr 8, 2009, at 2:10 PM, Peter Ammon wrote:
On Apr 8, 2009, at 2:47 PM, Rua Haszard Morris wrote:
I'm baffled, this seems fundamental, I can't see how it's done!
I have a Menu in a nib file, which I need to use in a few places in
code. So I want to do something like
NSMenu* myMenu = [NSMenu menuFromNib:@"MyNibFile"
name:@"MyUsefulMenu"];
Is this possible - how can this be done?
I have looked at the NSMenu reference, how to unarchive Carbon menus
(but there is no toll free mapping/other conversion between MenuRef
and NSMenu?). It seems the only way to get this working is to have
an class with an outlet specifically for receiving a reference to my
menu - which seems indirect...
Hopefully this is a dumb question and there's an easy answer...
Making a class with an outlet, and passing an instance of that class
as the nib's owner, is one way.
You can also use the NSNibTopLevelObjects key as described at the
bottom of http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSNib_Class/Reference/Reference.html
. You pass an NSMutableArray under this key, and after the nib is
loaded, the array is populated with the top level objects in the nib.
I do something similar, but not for a menu (I am doing this for a
NSView).
In my controller object's class initialization, I create a static
instance of a Nib that I create multiple instances of (using NSNib's -
initWithNibNamed). Then, whenever I need another instance of the Nib,
I invoke -instantiateNibWithOwner:topLevelObjects on the static Nib
instance. Doing so causes -awakeFromNib to be invoked for the
controller (beside the Nib) because the controller is the Files Owner
(set as an outlet in IB).
If you do this, and your controller is also instantiated from a NIb,
then you must be careful in your controller's -awakeFromNib to
distinguish its initial awakening from subsequent calls where it is
the File's Owner.
btw: you can pass nil as the parameter for topLevelObjects: if you
don't need the array of them (which I don't, as I do all the
initialization of the instantiated Nib instance in the -awakeFromNib
of the owner).
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden