首页 > 解决方案 > 如果初始窗口没有标题栏,如何关闭它

问题描述

(Swift、macOS、故事板)

如果没有标题栏,如何关闭初始窗口?

这行代码关闭了初始窗口。但前提是它有标题栏。我把它放在 AppDelegate.swift > func func applicationDidFinishLaunching 中:

NSApplication.shared.keyWindow?.close()

如果窗口没有标题栏也没有关闭按钮,是否还有其他选项可以关闭初始窗口?

(请注意,我们可以使用代码关闭之前打开的窗口。但是对于初始且没有标题的窗口,似乎有所不同)

一些澄清

似乎有些人不明白这个问题。我尝试提供更多细节。

//I can open a regular window:
var controller: NSWindowController?
let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
controller = storyboard.instantiateController(withIdentifier: "identifier2") as? NSWindowController
controller?.showWindow(self)

//Later I can close that window
controller?.close()

此关闭窗口的代码仅适用于我打开的窗口。不适用于初始窗口,默认打开的窗口。它是否具有标识符并不重要。

要关闭初始窗口,这行代码效果很好,但前提是窗口有标题栏:

//I can close the initial window if it has Title bar. 
//I put this in func applicationDidFinishLaunching:  
NSApplication.shared.keyWindow?.close()

所以,我的问题是:如何关闭初始窗口,如果该窗口没有标题栏。

一些定义:

标签: swiftmacosstoryboard

解决方案


有点模糊,所以我会尝试给出一个通用的指针:

由于这是你程序的最后一个窗口,一旦它消失就什么都不会留下,你可以使用 NSApplication.shared().terminate(self) 吗?


推荐阅读