On Jun 12, 2005, at 5:19 PM, Maxwell Robertson wrote: Under the old OS we had MBAR resources where you could define different menus and load them as needed.
Is it possible to do this with Cocoa and IB?
For example you have an application when it launches you see the MainMenu defined in the default MainMenu.nib. However your App has a different mode of working that involves switching all menus, the classic being Paint and Draw modes. I want to replace all menus then switch back as needed - note the new menu is based on data the user has entered.
I can step down the menus and delete them and add new ones but it seems like a lot of work.
I have still to figure out how to reload the MainMenu from the Nib once more. After deleting and then adding new menus, restoring the original with
NSMenu *newMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"]; [NSApp setMainMenu:newMenu];
results in an empty menu with only the Apple Menu being present.
1) Insert these instance variables into your .h file:
IBOutlet NSMenu *paintMenu; IBOutlet NSMenu *drawMenu;
NSMenuItem *paintMenuItem; NSMenuItem *drawMenuItem;
2) Insert these method definitions into your .h file:
- (void) goToDrawMode; - (void) goToPaintMode
3) Insert this code into your .m file:
- (void) awakeFromNib { paintMenuItem = [[NSMenuItem alloc] initWithTitle:@"Paint" action:NULL keyEquivalent:@""]; [paintMenuItem setSubmenu:paintMenu];
drawMenuItem = [[NSMenuItem alloc] initWithTitle:@"Draw" action:NULL keyEquivalent:@""]; [drawMenuItem setSubmenu:drawMenu]; }
- (void) dealloc { [paintMenuItem release]; [drawMenuItem release]; [super dealloc]; }
- (void) goToDrawMode {
NSMenu *mainMenu = [NSApp mainMenu]; [mainMenu removeItem:paintMenuItem];
// take help menu into account so subtract 2 int index = ([mainMenu numberOfItems] -2); [mainMenu addItem:drawMenuItem atIndex:index]; }
- (void) goToPaintMode { NSMenu *mainMenu = [NSApp mainMenu]; [mainMenu removeItem:drawMenuItem];
// take help menu into account so subtract 2 int index = ([mainMenu numberOfItems] -2); [mainMenu addItem:paintMenuItem atIndex:index];
}
4) Call -goToDrawMode to go to show the draw menu or -goToPaintMode to show the paint menu.
Disclaimer: This code was written in Mail.app and is not tested.
Eric Brunstad eric (at) mindsprockets.com Mind Sprockets Software www.mindsprockets.com |