views:

310

answers:

1

I need to create many Cocoa items programatically and most resources I can find focus on creating GUI with the builder. Is there any way to get the objective C code for an interface item created using the interface builder in XCode? Also, I am a little confused about PopUpMenus. Are PopUpMenus contained inside PopUpButtons or are the independent? Also, can anyone link me to a good description of how to create PopUpMenus programatically Using Cocoa and Objective C?

+1  A: 

It depends on what you mean by "PopUpMenus". In Cocoa a menu is a menu, specifically an instance of NSMenu. NSPopupButtons have an associated NSMenu object which you can assign to the button using the -setMenu: method.

You can certainly construct menus programmatically using the NSMenu and NSMenuItem classes. Take a look at the various -addMenuItem: methods in the NSMenu documentation.

You can also access the components of a menu created in Interface Builder programmatically if you have a reference to the menu, specifically an outlet instance variable that is connected to the menu in Interface Builder.

If you're referring to contextual menus that appear when you click the right mouse button, all you need to do is connect your menu to the view/control you want to attach it to with the -setMenu: method. You can make this connection in Interface Builder also.

Rob Keniger