0
votes

In one of my first Cocoa applications I have just a single window with 2 NSTextField instances - for user text input and of output of text processing.

If my user clicks on the red x on the top left, the window closes but the application is still running and icon stays in the dock - the normal Mac behavior.

When the user double-clicks on the icon in dock or on the desktop, this does not reopen the window - apparently also normal Mac behavior, but confusing to the user.

To get the app back into a running state, the user has to force Quit from the main menu or the context menu, and restart the app by clicking on one of the icons.

I searched Apple doc and forums, and it seemed that the following should prevent the closing of the window (my first preference : hide the widow so it can be reopened later) :

  1. add a delegate to NSApp

  2. delegate implements -applicationShouldHandleReopen which calls [mainWindow makeKeyAndOrderFront:self]; and returns TRUE

  3. delegate implements -windowShouldClose which returns FALSE However, although -windowShouldClose is called, the window closes.

What am I missing here?

As an alternative (my second preference), I added to the delegate

  1. -applicationShouldTerminateAfterLastWindowClosed which returns YES

This works, i.e. the application closes when the used clicks on the red x, and the user can restart the app later without further ado.

Clarifications and pointers to specific doc and working code examples would be appreciated.

Rudi

2

2 Answers

2
votes

"When the user double-clicks on the icon in dock or on the desktop, this does not reopen the window - apparently also normal Mac behavior, but confusing to the user."

If you want the window to re-open in that case, implement applicationShouldHandleReopen:hasVisibleWindows:. There's nothing un-Mac-like about opening a window when the user clicks the dock icon after closing all the windows; lots of apps do it and the delegate exists specifically to support that behavior.

1
votes

First of all, your "alternative" behavior of terminating the app on window close is probably the preferred approach for your situation. Users may be confused when they can't close the window.

If you really want to prevent the window from being closed, why not just disable the close control on the window in IB?