使用通知中心时,我似乎得到了一个强参考周期。
我正在使用 NotificationCenter 来观察设备的旋转。(虽然有些人认为这不是确定设备旋转的最佳方式,但目前这似乎是我唯一的途径,因为没有使用自动布局,也没有使用情节提要)。
deinit {}
ViewController
即使我删除了 and 中的观察者,viewWillDisappear
也永远不会在 my 中调用viewDidDisappear
。
import UIKit
class TestVC: UIViewController {
deinit {
print("TestClass Deinit") //not being triggered ever
}
@objc private func rotationDetected(sender: Any) {
print("we rotated")
}
override func viewDidDisappear(_ animated: Bool) {
//NotificationCenter.default.removeObserver(UIDevice.orientationDidChangeNotification)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(UIDevice.orientationDidChangeNotification)
//NotificationCenter.default.removeObserver(self) //also doesn't work
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: rotationDetected)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
关于为什么会发生这种情况以及如何解决它的任何想法?
也欢迎任何关于如何确定旋转检测的新想法(虽然没有使用自动布局或情节提要)。
要达到TestVC()
我 self.navigationController?.pushViewController(TestVC(), animated: true)
在以前使用过ViewController
并返回我使用pop
.
没有Observer
现在,班级将正确deinit
。
解决
由于下面标记的答案,强参考循环被删除。
只需更换NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: rotationDetected)
和
NotificationCenter.default.addObserver(self, selector: #selector(rotationDetected), name: UIDevice.orientationDidChangeNotification, object: nil)