我一直在关注一个视频教程,该视频教程教我如何以编程方式创建没有 Storyboard的 iOS 应用程序。
进展顺利,我喜欢它的大部分内容,但我有一个小问题。
我只读了一半,我注意到一个潜在的内存问题,教程向我展示的方式是,对于每个下一个 View ,我都这样呈现:
let ChooseCategoryController = ChooseCategoryViewController()
present(ChooseCategoryController, animated: true, completion: nil)
然后我从那里的角度展示另一个。回过头来看,我也提出了另一种观点。我在运行应用程序时注意到内存使用量在不断增加。
有没有一种方法,当我呈现一个新 View 时,我可以检测到哪些 View 是打开的并关闭所有其他 View ?
我想在完成部分可能会有一些东西,但我很挣扎,因为我很新。
提前致谢。
更新1
在尝试下面的解决方案并添加 self.正如 xcode 要求我有这段代码:
dismiss(animated: false, completion: {
self.present(DisplayQuestionsViewController(), animated: true, completion: nil)
})
我收到这个警告:
Warning: Attempt to present < triviaGameApp.DisplayQuestionsViewController: 0x7fb39481ce00 > on < triviaGameApp.ChooseCategoryViewController: 0x7fb392e019f0 > whose view is not in the window hierarchy!
如果相关。在 ChooseCategoryViewController() 上单击 UICollectionView 单元格时会触发此代码
更新 2:
我现在将代码更改为以下内容:
dismiss(animated: false, completion: {
self.parent?.present(DisplayQuestionsViewController(), animated: true, completion: nil)
})
正如下面的海报所提到的。当前的 View Controller 现在确实关闭了,但是它不会加载新的 View Controller 。
最佳答案
你可以,但你必须循环遍历 windows
中的所有 viewControllers
并将其删除。我会说这很危险,真的不推荐。
相反,我想建议这个。您关闭
您的旧 Controller ,同时呈现
一个新 Controller 。
dismiss(animated: false, completion: {
self.parent?.present(MyOtherViewController(), animated: true, completion: nil
})
上面的代码使用completion
block 来确保当当前的ViewController
被关闭时,它会立即present
下一个在本例中为 MyOtherViewController
。
关于ios - 关闭所有非当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40787677/