这里是新的 StackOverflow 用户(第一次发帖,长时间潜伏在没有帐户的情况下)。在开始之前,这些是我之前回答过的一些问题,我发现它们很有帮助,但还没有完全解决我的问题:
为 NSNotificationCenter = Swift deinit() 调用 .removeObserver 的正确位置?
从这些我构建了一个 BaseView 控制器,用它来控制我的应用程序在各种情况下的行为(例如,当应用程序回到前台时检查更新的 API 调用)
class BaseViewController : UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
@objc func applicationWillEnterForeground() {
}
@objc func applicationDidEnterBackground() {
}
deinit {
print("WORKING - deinit BaseViewController")
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
}
}
但是,我的问题是我需要使用其他 NotificationCenter 观察者来动态控制导航(进度)栏,该栏取决于用户在应用程序中的位置(以及他们在那里做什么,与其他区域隔离)。
我的问题是:“调用 .removeObserver 的正确位置是否总是 deinit()?” 或者,如果没有,是否有任何关键的地方应该考虑添加 .removeObserver 调用?
如果有帮助,应用程序每个部分的导航栏都附加到(a) 上,该MainPagerVC
(a UIPageViewController
) 可通过LGSideMenuController重用和切换进出