简体   繁体   English

当点击时,你如何让Dock图标显示一个窗口?

[英]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. 我希望Dock Icon在关闭后使用方法makekeyandorderfront打开Main窗口。 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. 我用一个按钮打开一个窗口,但我不知道如何使用Dock图标,因为你无法在界面构建器中建立它。

There's a delegate method in NSApplication's delegate: NSApplication的委托中有一个委托方法:

- (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 将此delagate方法放在您的app委托中

- (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. 我更喜欢将这种方法用于非基于文档的应用程序,因为当关闭它的唯一窗口时,让应用程序运行是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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