macos 如何以编程方式退出 mac 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6989260/
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-10-21 08:17:33  来源:igfitidea点击:

How do I quit a mac application programmatically?

objective-ccocoamacos

提问by Man of One Way

I need to add a quit-button to my application that runs from the menubar in mac. How do I programmatically quit an application in mac?

我需要向我的应用程序添加一个退出按钮,该按钮从 mac 的菜单栏中运行。如何以编程方式退出 mac 中的应用程序?

回答by sidyll

There is a simpler form to quit from code:

有一种更简单的形式可以退出代码:

[NSApp terminate:self];

But as you're adding a button, all you have to do is to control drag from your button to the Application icon and connect the method terminate:.

但是当您添加按钮时,您所要做的就是控制从按钮到应用程序图标的拖动并连接方法terminate:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by omz

[[NSApplication sharedApplication] terminate:self];

回答by Nathanial Woolls

Try the following:

请尝试以下操作:

[NSApp terminate: nil];

回答by larva

In some case you cannot close app when call [NSApp terminate:self];. Like when showing NSAlert as sheet on the doc window (NSAlert -beginSheetModalForWindow:completionHandler:) ...

在某些情况下,您无法在调用时关闭应用程序[NSApp terminate:self];。就像在文档窗口 ( NSAlert -beginSheetModalForWindow:completionHandler:)上将 NSAlert 显示为工作表一样......

You can close all window and alert before call terminate, like following code:

您可以在呼叫终止之前关闭所有窗口和警报,如下代码所示:

for (NSWindow *window in [NSApplication sharedApplication].windows) {
    [window close];
}
[NSApp terminate:self];