views:

348

answers:

2

This Is Similar To One Of My Earlier Questions That Never Got Answered. I would like the Dock Icon to use the method makekeyandorderfront to open the Main window after it has been closed. I have done this with a button opening a Window but I don't know how to do it with the Dock Icon because you can't make a connection to it in interface builder.

+8  A: 

There's a delegate method in NSApplication's delegate:

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag;

which you can use to handle clicks on the app's icon in the dock. See its documentation.

Nikolai Ruhe
Thanks for that, What code would I need to make it open a window though?
Joshua
Thanks for that, What code would I need to make it open a window though?
Joshua
Please! What code would I need to make it open a window though?
Joshua
Just to make you stop nagging: [_window makeKeyAndOrderFront:nil]; Of course you need the instance variable in the delegate's class.B.t.w.: SO is for coding questions. If you need complete code examples to copy and paste there are other sites on the web.
Nikolai Ruhe
Sorry, what other websites are there for complete code examples?
Joshua
What would I need to do to make the the app know what window to open when the dock is clicked?
Joshua
+4  A: 

There is another way to handle this:

Rather than try and re-open an app window by clicking in the dock you could tell the app to terminate when the last window is closed. This way, you won't have an active dock icon showing when there are no windows open.

Put this delagate method in your app delegate

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
    return YES;
}

Which method you chose, though, is up to you and your app. I prefer to use this method with non document based apps because it doesn't make sense to have your app running when the only window for it is shut down.

Abizern
Thanks for the code, but that wouldn't suit my application, but it's good to know that this is possible also. Thanks.
Joshua
Don't think of it as just code. It's an example of using one of the many NSApplication delegate methods.
Abizern