title: Best way to toggle "Show/Hide" menu item in Cocoa Desktop
body: <p>My app has an 'inspector' panel defined by a .xib file and a custom window controller class: <code>AdjustmentsWindow.xib</code> and <code>AdjustmentsWindowController.m</code>.</p>
  
  <p>I want to have a <code>Window -&gt; Show Adjustments</code> menu item in the application's main menu bar, that when selected will show the adjustments window. I dropped and NSObject instance into the xib containing the main menu, and changed its class to "AdjustmentsWindowController". I also hooked the menu item's <code>action</code> to the controller's <code>-showWindow:</code> method. So far so good: The window controller is instanced on app launch, and when you select the menu item it shows its window.</p>
  
  <p>But I want the same menu item to double as 'Hide Adjustments' when the window is already visible (effectively toggling visibility). So here is what I did:</p>
  
  <p><strong>AdjustmentsWindowController.m:</strong></p>
  
  <pre><code>- (void) windowDidLoad
  {
      [super windowDidLoad];    
      [[self window] setDelegate:self];
  }
  
  - (void) showWindow:(id)sender
  {
      // (Sent by 'original' menu item or 'restored' menu item)
  
      [super showWindow:sender];
  
      // Modify menu item:
  
      NSMenuItem* item = (NSMenuItem*) sender;
      [item setTitle:@"Hide Adjustments"];
      [item setAction:@selector(hideWindow:)];
  }
  
  
  - (void) hideWindow:(id) sender
  {
      // (Sent by 'modified' menu item)
  
      NSMenuItem* item = (NSMenuItem*) sender;
  
      // Modify back to original state:
  
      [item setTitle:@"Show Adjustments"];
      [item setAction:@selector(showWindow:)];
      [self close];
  }
  
  - (void) windowWillClose:(NSNotification *)notification
  {
      // (Sent when user manually closes window)
  
      NSMenu* menu = [[NSApplication sharedApplication] mainMenu];
  
      // Find menu item and restore to its original state
  
      NSMenuItem* windowItem = [menu itemWithTitle:@"Window"];
      if ([windowItem hasSubmenu]) {
          NSMenu* submenu = [windowItem submenu];
          NSMenuItem* item = [submenu itemWithTitle:@"Hide Adjustments"];
          [item setTitle:@"Show Adjustments"];
          [item setAction:@selector(showWindow:)];
      }
  }
  </code></pre>
  
  <p>My question is, is this the right/smartest/most elegant way to achieve this? I mean, this is pretty standard behaviour in cocoa apps (cf. Numbers' "Inspector"), how does everyone else do it?</p>
  
  <p>One way to improve it would be to avoid the code duplication of restoring the menu item to its original title/action. Also, ideally I would replace the title strings with calls to <code>NSLocalizedString()</code>. But perhaps there's a more elegant, standard approach that I don't know...</p>