ios - 从 subview 中删除观察者

标签 ios swift observers

我有一个 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/

相关文章:

swift - 如何将 String 转换为 UnsafeMutablePointer<UInt8> ? swift 3

design-patterns - 将主体映射到观察者 - 观察者模式 GoF 书籍

iphone - 从 NSOperationQueue 中取消 NSOperation 导致崩溃

ios - 如何对 AFNetworking 请求进行单元测试

ios - 为什么这个布局模棱两可?

ruby-on-rails-3.2 - 是否可以有一个邮件观察者只处理特定的邮件程序?

php - Laravel 5.1 - 相关模型上的模型观察者

iphone - iOS Mail中图像尺寸的减小

ios - Objective C 框架/静态库可以由第三方框架组成吗

ios - 与 UITableView 的 super View 上的 UIPanGestureRecognizer 冲突