• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: menu question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: menu question


  • Subject: Re: menu question
  • From: Eric Brunstad <email@hidden>
  • Date: Sun, 12 Jun 2005 17:51:40 -0400


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
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >menu question (From: Maxwell Robertson <email@hidden>)

  • Prev by Date: Re: how to make an NSWindow unmovable...
  • Next by Date: Re: how to make an NSWindow unmovable...
  • Previous by thread: menu question
  • Next by thread: One Solution for storing ordered arrays within Core Data objects
  • Index(es):
    • Date
    • Thread