swift - 手动删除 iOS 9.0 及更高版本的通知观察器
问题描述
我想知道是否可以删除 iOS9.0 及更高版本的通知观察者。苹果文档说,一旦视图消失,通知将自动删除,这是真的,但并不完全正确。即使视图消失了,我的 tarBarController 也会调用 UIApplication.willResignActiveNotification 。
如果 tarBarController 提供了presentViewController,则presentViewController 调用通知并且tarBarController 执行相同的操作,即使我删除了tarBarController 中的观察者
//TabBarController
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if #available(iOS 13.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillDisappear(notification:)), name: UIScene.willDeactivateNotification, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillDisappear(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
NotificationCenter.default.removeObserver(self) // there is no need to remove this observer in iOS 9 and later , but TarBarController doesn't dealloc
}
//PresentedViewController
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if #available(iOS 13.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillDisappear(notification:)), name: UIScene.willDeactivateNotification, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillDisappear(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// the presentedViewController removes this observer which is fine
}
解决方案
推荐阅读
- .htaccess - .htaccess : 从基于 TLD 的文件夹加载
- r - 如何将行条目更改为“未知”?
- python - 有没有办法将特殊的非字符键作为字符串类型转换为键类型?(例如“Key.f12”到实际的keyboard.Key.f12值)
- google-cloud-dataflow - GCD中内存使用的奇怪图表
- html - jquery Uncaught SyntaxError:无效或意外的令牌 - 错误
- regex - 正则表达式从最后一个中提取单词
- python - 基于键的字典交集,TypeError:列表索引必须是整数或切片,而不是索引
- flutter - Flutter 显示 JSON 数组中的值并点击更改索引
- selenium - Azure devops 硒测试计划
- java - 如何在运行时从父类访问子类变量?