[英]Cocoa app: how to close a window without terminating the app?
I am working on a Cocoa app, using Swift. 我正在使用Swift开发可可应用程序。 The app opens with a window and the user can open another window using the menu.
该应用程序将打开一个窗口,用户可以使用菜单打开另一个窗口。 The problem is that when either window is closed using the red button, the app terminates.
问题是,当使用红色按钮关闭任一窗口时,应用程序将终止。 I don't want that behavior;
我不要这种行为; I want the app to remain alive.
我希望该应用程序继续运行。 It's not crashing;
它没有崩溃; the AppDelegate's applicationWillTerminate method executes.
AppDelegate的applicationWillTerminate方法执行。
The AppDelegate has the method applicationShouldTerminateAfterLastWindowClosed, which returns false, but this method never gets executed. AppDelegate具有方法applicationShouldTerminateAfterLastWindowClosed,该方法返回false,但是此方法永远不会执行。
I am a reasonably competent Swift iOS developer; 我是一名称职的Swift iOS开发人员; this is my first real Cocoa app and I suspect this is something simple, but I'm baffled right now.
这是我第一个真正的Cocoa应用程序,我怀疑这很简单,但是我现在感到困惑。 How do I close a window without terminating the app entirely?
如何在不完全终止应用程序的情况下关闭窗口?
Edit: Here are some code snippets: 编辑:这是一些代码片段:
In the AppDelegate: 在AppDelegate中:
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
Log.methodEnter()
return false
}
Also in the AppDelegate, the method to open a window: 同样在AppDelegate中,打开窗口的方法:
@IBAction func openVerbSettingsWindow(sender: NSMenuItem) {
verbSettingsWindowController.showWindow (nil)
}
From the windowViewController, the only method that does anything as the window loads and displays: 在windowViewController中,唯一的方法是在窗口加载并显示时执行任何操作:
override func windowDidLoad() {
super.windowDidLoad()
window?.setFrame(NSMakeRect(400.0, 100.0, 950.0, 450.0), display: true)
}
I'm hopeful this helps! 我希望这会有所帮助!
I'm not a little embarrassed at the answer to this question; 这个问题的答案我一点也不为难。 it has nothing to do with Cocoa or my code.
它与可可或我的代码无关。
I have an app called RedQuits that configures OS X to close an app when that app's last window closes, rather than leaving the app running with no windows (OS X default behavior). 我有一个名为RedQuits的应用程序,该应用程序将OS X配置为在该应用程序的最后一个窗口关闭时关闭该应用程序,而不是使该应用程序没有窗口运行(OS X的默认行为)。 When I got rid of RedQuits and re-booted so that OS X would return to its default behavior, the problem went away.
当我摆脱RedQuits并重新启动以便OS X返回其默认行为时,问题就消失了。 For some reason, RedQuits causes my Cocoa app to quit when any application window closes.
由于某些原因,RedQuits会导致我的Cocoa应用程序在任何应用程序窗口关闭时退出。 Interestingly, I found this because it causes the same behavior in Xcode: close one project window and Xcode shut down.
有趣的是,我发现了这一点,因为它在Xcode中引起了相同的行为:关闭一个项目窗口,然后关闭Xcode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.