gpt4 book ai didi

swift - 从 appDelegate 观察 .effectiveAppearance 变化

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:49 27 4
gpt4 key购买 nike

在我的 appDelegate 的 applicationDidFinishLaunching 方法中,我想观察亮/暗模式下的外观变化。

这些似乎都不起作用:

NSApp.observe(\.effectiveAppearance) { _, _ in
print("it works!")
}

NSApplication.shared.observe(\.effectiveAppearance) { _, _ in
print("it works!")
}

我如何检测这种变化?

最佳答案

你只需要通过将它分配给类级别对象来保持这个观察者存在,如下所示,

class AppDelegate: NSObject, NSApplicationDelegate {


private var observer: Any!

func applicationDidFinishLaunching(_ aNotification: Notification) {

self.observer = NSApp.observe(\.effectiveAppearance) { _, _ in
print("it works!")
}
}
}

关于swift - 从 appDelegate 观察 .effectiveAppearance 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com