我很难让我NSView
观察其 parent 的变化NSWindow
。我在一个测试项目上尝试了这个,它只NSWindow
包含一个MyView
子类,NSView
只是为了看看我是否遗漏了一些东西。
class MyView: NSView {
//add observer on awake from nib
//works fine
override func awakeFromNib() {
if let win = self.window {
win.addObserver(self, forKeyPath: Notification.Name.NSWindowDidBecomeKey.rawValue, options: .new, context: nil)
}
}
//remove observer on dealloc
//allso works fine
deinit {
if let win = self.window {
win.removeObserver(self, forKeyPath: Notification.Name.NSWindowDidBecomeKey.rawValue)
}
}
//never gets called
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
Swift.print(keyPath ?? "")
Swift.print("worked!")
}
override func draw(_ dirtyRect: NSRect) {
NSColor.blue.set()
NSBezierPath.fill(dirtyRect)
}
}
我也尝试过这里的建议,但没有运气。我还检查了其他答案,但找不到答案。大多数涵盖使您自己的类 KVO 兼容。我想观察一个 NSWindow 对象。我错过了什么?