gpt4 book ai didi

ios - 在导航堆栈中用观察者替换 Controller 时应用程序终止

转载 作者:行者123 更新时间:2023-11-30 12:33:01 24 4
gpt4 key购买 nike

我的应用程序的第一个 Controller 有一个 avplayer,因此我实现了在 viewWillAppear 上设置并在 viewDidDisappear 上删除的观察者。

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
addObservers()

}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
avQueuePlayer.pause()
// remove observers
NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil)
}

当我收到深层链接时,我会创建一个新的导航堆栈,然后用新的导航 Controller 堆栈替换当前的导航 Controller 堆栈:

            navigationVC.viewControllers = newNavigationVC.viewControllers

应用程序崩溃了,而不是正确加载新 Controller (如果我删除观察者的代码,就会正确加载),应用程序崩溃了:

libc++abi.dylib: terminating with uncaught exception of type NSException

我不知道发生了什么,因为 xcode 没有给我任何指示,并且跟踪是在汇编中,所以它也没有帮助。

有人可以指出我调试此问题的正确方向吗?

最佳答案

通常,一次删除所有观察者更有意义。

NotificationCenter.default.removeObserver(self)

关于ios - 在导航堆栈中用观察者替换 Controller 时应用程序终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43234804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com