作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在我的 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/
在我的 appDelegate 的 applicationDidFinishLaunching 方法中,我想观察亮/暗模式下的外观变化。 这些似乎都不起作用: NSApp.observe(\.effe
我是一名优秀的程序员,十分优秀!