Generally, I find that working with NSWindowControllers
makes dealing with windows much simpler. In your case, your App Delegate wouldn't own the window directly, but would instead own the NSWindowController
that owns the window.
To do this, you'd create a new NSWindowController
subclass (with a xib for the user interface), and the migrate your window from the MainMenu.xib into your new WindowController.xib. Hook things up, and then you're ready to go. (also, make sure the "Visible at launch" checkbox on your window is unchecked)
From your app delegate, you'll then just create a new instance of the window controller (whether programmatically or via MainMenu.xib doesn't matter), and tell it to -showWindow:
when you want the window to show (or become key), and -close
when you want it to go away.
As for responding to clicking on the Dock icon, the <NSApplicationDelegate>
method you're looking for is -applicationShouldHandleReopen:hasVisibleWindows:
.