我有一个 View ,其中有一个下拉菜单作为 subview 。现在,当 View 被释放时,我收到一个错误。 “在关键值观察者仍在其中注册时被解除分配”
在 View 类中,我设置观察者:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
self.setNeedsDisplay()
if keyPath == "frame" {
// Set up DropdownMenu
self.dropDownBackground.frame.size.height = self.dropDownMenu.frame.maxY
}
}
并将其添加到 subview 中:
dropDownMenu.addObserver(self, forKeyPath: "frame", options: .New, context: nil)
因此,为了避免我正在尝试执行的错误:
deinit {
dropDownMenu.removeObserver(self)
}
但是编译器告诉我“无法使用参数列表“DropDownMenu”调用“removeObserver””
我做错了什么?谢谢!
最佳答案
有一个look at the documentation :该方法被称为removeObserver(_observer: NSObject, forKeyPath keyPath: String)
。所以你需要这样做:
dropDownMenu.removeObserver(self, forKeyPath: "frame")
关于ios - 从 subview 中删除观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37773215/