当用户关闭应用的首选项时,我试图简单地调用我的 AppDelegate 中的一个函数,即下面的 ContentView
。
onDisappear
中的断点由于某种原因从未命中。我很乐意帮助您理解为什么会这样。
struct ContentView: View {
@EnvironmentObject var state: AppState
var body: some View {
Text("Hi there")
.onDisappear {
// Never gets in here, breakpoint never hits
let appDelegate = NSApplication.shared.delegate as! AppDelegate
appDelegate.hideIcon()
}
}
在我的 AppDelegate 中:
func hideIcon() {
NSApp.setActivationPolicy(.accessory)
}
如何启动内容查看:
@objc func preferences() {
let contentView = ContentView().environmentObject(state)
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable],
backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Arch OS System Preferences")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
NSApp.setActivationPolicy(.regular)
NSRunningApplication.current.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])
}
谢谢!