swiftui - 关闭最后一个窗口时关闭 SwiftUI 应用程序
问题描述
是否可以在用户关闭最后一个窗口时关闭 macOS SwiftUI 应用程序,类似于applicationShouldTerminateAfterLastWindowClosed
AppDelegate 函数。
func applicationShouldTerminateAfterLastWindowClosed(NSApplication) -> Bool
解决方案
我在这里找到了答案https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
为 AppDelegate 创建一个类
import Foundation
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
将属性包装器添加到您的 SwiftUI App 类
import SwiftUI
@main
struct SwiftUIApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 300, idealWidth: 300, maxWidth: .infinity, minHeight: 300, idealHeight: 300, maxHeight: .infinity)
}
}
}
推荐阅读
- jquery - HighMaps 未定义的 ZoomTo()
- android - 以编程方式为旧 API 创建形状背景
- c# - ListView 未添加项目
- http - 使用单位 LazUTF8 时 TIdURI.URLDecode 的错误结果
- chatbot - 使用botium box从bot获取变量输出时测试用例失败
- networking - 是否需要在 SPF 记录本身中添加相同的域名?
- opengl - 无法创建超过 8 个渲染缓冲区的 FBO
- html - 尽管使用了@media 查询,但文本拒绝调整大小
- python - 关于优化熊猫脚本删除for循环的问题(iterrows,zip,...)
- javascript - 链接同步 Redux 操作并在组件中使用