我的应用程序中有很多 ViewController,我可以根据需要成功推送和弹出它们。 navigationController 可用并完成它的工作。
但是过了一段时间,它被iOS自动设置为nil。我不知道代码中发生了什么,更有趣的是,这怎么可能?
navigationController 不是一个在整个应用程序存在期间都存在的对象吗?
如果我知道要寻找什么,我会分享一些代码。
那时,当 navigationController 变为 nil 时,我无法再推送任何 ViewController,唯一剩下的就是退出应用程序并重新启动它,但这不是我可以要求用户做的事情。
那么,是什么让 iOS 将一个正常工作的 navigationController 设置为 nil?
我在 Logcat 中没有看到任何异常,也没有崩溃或相关警告。
如果您的 VC 实际上是该导航控制器的导航堆栈的一部分,请确保 only 期望设置
navigationController
。
意思是:你是用
.setViewControllers(...)
还是 .pushViewController(...)
来显示那个特定的 ViewController?
或者你更愿意用.present(...)
来展示VC?因为如果您呈现,ViewController not 是导航堆栈的一部分。 (因此,它也不会有可见的导航栏或后退按钮。)
如果您需要一个 navigationController 来将某些内容推送到呈现的 ViewController 中,首先将该 VC 包装在一个新的 NavigationController 中,然后在您之前存在的 navigationController 中呈现新的 navigationController。