xcode 使用窗口关闭按钮完全关闭 OS X 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8233141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:31:44  来源:igfitidea点击:

Completely close an OS X application with window close button

objective-cxcodemacos

提问by DevCompany

I need completely close my application when a user closes the document window using the applications at runtime.

当用户在运行时使用应用程序关闭文档窗口时,我需要完全关闭我的应用程序。

Currently when they click the window, the application stays open and is displaying its menu bar. I read this article for iOS and to add an entry into the plist file: http://developer.appcelerator.com/question/40081/ios4---make-app-not-run-in-background

当前,当他们单击窗口时,应用程序保持打开状态并显示其菜单栏。我为 iOS 阅读了这篇文章,并在 plist 文件中添加了一个条目:http: //developer.appcelerator.com/question/40081/ios4---make-app-not-run-in-background

What is the easiest way for me to close the entire app when a user closes the document window?

当用户关闭文档窗口时,我关闭整个应用程序的最简单方法是什么?

回答by mipadi

Is this a Mac app? If so, have your NSAppdelegate implement applicationShouldTerminateAfterLastWindowClosed::

这是 Mac 应用程序吗?如果是这样,请让您的NSApp委托实施applicationShouldTerminateAfterLastWindowClosed:

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

Using Swift, it would be:

使用 Swift,它将是:

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
     return true
}

回答by David

When the button is pressed:

当按钮被按下时:

[NSApp terminate:self];

回答by Erfan

Add the NSWindowDelegatein YourWindowController.hfile and then add the following method in YourWindowController.mfile:

添加NSWindowDelegateinYourWindowController.h文件,然后在文件中添加以下方法YourWindowController.m

-(void)windowWillClose:(NSNotification *)notification
{
    // You can use either of the following lines:
    [[NSApplication sharedApplication] terminate:nil];

    // OR
    exit(0);
}

You will need to use the NSWindowDelegate for all your windows.

您将需要为所有窗口使用 NSWindowDelegate。