swift - 如果初始窗口没有标题栏,如何关闭它
问题描述
(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()
所以,我的问题是:如何关闭初始窗口,如果该窗口没有标题栏。
一些定义:
- 是初始控制器。您可以看到,如果您选择 Storyboard > Window Controller > Attributes
- 标题栏。故事板 > 窗口 > 显示标题栏和控件 > 关闭。两个选项均未选中。
解决方案
有点模糊,所以我会尝试给出一个通用的指针:
由于这是你程序的最后一个窗口,一旦它消失就什么都不会留下,你可以使用 NSApplication.shared().terminate(self) 吗?
推荐阅读
- java - 如何使用结果集排序?
- sql-server - 如何修复巨大的日志文件问题
- spring-integration - 向 ibm mq 发送 amqp 消息
- javascript - 如何通过 webkit 使 JS 与通过 chromedriver 一样工作?
- symfony - 奏鸣曲管理员:user_block 未显示
- java - 有没有“重班”之类的东西。它的正确定义是什么?
- html - 禁用角度形式的输入
- node.js - 如果在数据集末尾,请检查 nextPage 是否不起作用
- php - 备份文件重复,名称相同
- angular - 如何使用花括号在 Angular 中导入 openlayers 模块