tags:

views:

784

answers:

3

I'm surprised this doesn't happen automatically, but I would like my applications window to open automatically when the Dock icon is clicked.

Just to clarify, when i open the app, the window automatically opens, but when I click the cross for the window but leave the App running, the window won't open when i click the dock icon.

+3  A: 

A document based application will automatically open a new untitled document when the app becomes active, so I am assuming you are referring to a non-document based app.

Implement the applicationDidBecomeActive: method in your application delegate and open/show the window.

Edit:

Some information on Delegates.

Some information on Opening and Closing Windows and the NSWindow API

Nathan Kinsinger
I don't understand how I can implement the applicationDidBecomeActive: method. Can you give me a code snipped/example/
Joshua
Where is the checkbox for hide on load in IB?
Joshua
@Joshua - I'm sorry, it's the exact opposite. If you select the inspector for the Window in the nib file, make sure that "Visible at Launch" is checked.
Jason Coco
@Jason Check the first post, It opens fine when you first open it, but when you click the cross, but not close the App, and then click the dock icon the window does not automatically open.
Joshua
How is that going to help? Can you tell me how to make the window open again after it's been closed (not the app, the window) by clicking the dock icon?
Joshua
It works if you hide the window but not if you close the window. Which I think is because Cocoa is releasing the window if you close it. But the obvious checkbox in IB doesn't help. I'm doing more research.
Max Howell
+7  A: 

Implement - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag in your app delegate. Check the documentation for the details of the return value.

Document based apps and non-document based apps behave slightly differently. If there are no open windows when the dock icon of a document based app is clicked then it will create a new document. If there are no open windows when the dock icon of a non-document based app is clicked then it will do nothing.

Benedict Cohen
Thanks very much!
Joshua
A: 

This is what I'm doing to get the main window of a non-document based app back to screen once it has been closed. I know this might not be the right way to do it but It's working for me so far.

Implemented this on the AppDelegate, window is defined as instance variable of the same object.

- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [window makeKeyAndOrderFront:self];
    return NO;
}

If anyone has a better solution please reply. Thanks!

Kindred