当用户关闭应用程序的首选项时,我试图简单地调用 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()
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 AppDelegate 中:
func hideIcon() {
NSApp.setActivationPolicy(.accessory)
}
Run Code Online (Sandbox Code Playgroud)
如何启动内容视图:
@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])
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
| 归档时间: |
|
| 查看次数: |
691 次 |
| 最近记录: |