tags:

views:

354

answers:

2

Hello

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

+1  A: 

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?

kperryua
Following your suggestion, I disabled the close control in IB.Now the red x is grayed out, and the user can only minimize the app window with the orange '-' button, or close the app with Quit.This is fairly clear to the user.
rudifa
+2  A: 

"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.

smorgan
Yes, I agree that handling reopen like this is another great Mac-like approach. It really depends on what his application is, of course. If it's a document-based app, then keeping the app running and handling reopen is best (a la TextEdit). If it's a small utility app, I would say terminate it on close, and make the user relaunch the app.
kperryua
Yes, this is a small utility app.As I mentioned, I did succeed in implementing 'terminate it on close, and make the user relaunch the app' using delegate with -applicationShouldTerminateAfterLastWindowClosed which returns YES.What did not work for me was my implementation of 'opening a window when the user clicks the dock icon after closing all the windows' - maybe I missed some subtility.Thanks to both smorgan and kperryua
rudifa