2
votes

I'm writing an application with a main window that's displayed when the app starts. When the window is closed, I'd like the app to remain running (with a menu-bar menu), and if the user clicks on the dock icon again, I'd like the main window to be presented again.

I'm about 90% of the way there: my app properly keeps running after the main window is closed with Cmd-W, and since "Release When Closed" is unchecked, the window could be [makeKeyAndOrderFront:]-ed to show it again when the dock icon is clicked.

The only missing piece of this puzzle is intercepting the actual dock-icon click.

The other threads about this topic recommend implementing either applicationShouldHandleReopen:hasVisibleWindows: or applicationShouldOpenUntitledFile: in the window controller. I've done both, and neither one ever gets called.

Any other ideas?

2
What is the actual delegate assigned to NSApplication? That is where the methods should be implemented. I don't think window controllers automatically receive messages intended for the application delegate.Kevin Grant
@KevinGrant: They don't.Peter Hosey

2 Answers

2
votes

The other threads about this topic recommend implementing either applicationShouldHandleReopen:hasVisibleWindows: or applicationShouldOpenUntitledFile: in the window controller.

That's only true if the window controller is the application's delegate. That is the object to which the application sends those messages.

I would not make a window controller the application's delegate, though. I typically make them two separate objects. Make one object specifically to be the application's delegate, and when that object receives the relevant delegate messages, send a message to your window controller telling it to do whatever it needs to do.

Actually, what I usually do in single-window apps is make the application's delegate create and own the window controller. You can respond to window closure by throwing away the WC, and respond to reopen by checking whether you have a WC and creating one (and thereby reopening the window) if you don't.

0
votes

Use [NSApp setDelegate:self]; in awakeFromNib.