在appdelegate的applicationDidFinishLaunching
方法中,我想观察从亮/暗模式的外观变化。
这两种方法都不起作用:
NSApp.observe(\.effectiveAppearance) { _, _ in
print("it works!")
}
NSApplication.shared.observe(\.effectiveAppearance) { _, _ in
print("it works!")
}
我怎样才能察觉到这种变化?
最佳答案
你只需要把这个observer
赋给一个类级对象来保持它的活性,如下所示,
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/