简体   繁体   中英

How would you make The Dock Icon show a window when clicked?

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM