简体   繁体   English

在导航栏中转到新视图 controller,memory 不断上升

[英]Segue to new view controller within a nav bar, the memory keeps rising

I am learning and working on an ios app.我正在学习和开发 ios 应用程序。 I have a nav bar with buttons that are programmed to present other view controllers in the app.我有一个带有按钮的导航栏,这些按钮被编程为在应用程序中显示其他视图控制器。 Heres a sample of the code:下面是代码示例:

let vc = storyboard?.instantiateViewController(identifier: “ViewController”) as! ViewController
        
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)

As I navigate around the app while running the simulation the Memory keeps rising.当我在运行模拟时浏览应用程序时,Memory 不断上升。 Should I be clearing previous pages viewed so that the memory doesn't rise?我是否应该清除以前查看的页面以使 memory 不会上升?

Every time when presenting a new scene in your app, you as a developer are responsible for presenting and dismissing the scene properly.每次在您的应用程序中呈现新场景时,您作为开发人员都有责任正确呈现关闭场景。

There are many ways of presenting a scene.呈现场景的方式有很多种。

For example,The push -style navigation automatically contains/adds controls for backward navigation because it adds all ViewControllers to its navigation stack ;例如, push式导航自动包含/添加用于backward navigation的控件,因为它将所有 ViewControllers 添加到其navigation stack中; all ViewControllers will be embedded in the same NavigationController ;所有 ViewController 都将嵌入同一个NavigationController中; thus, when dismissing it, you have to write:因此,当解雇它时,你必须写:

navigationController.popViewController(animated: true)

The VC will be popped (removing an element from the top of the stack, newest element in the stack) and not just dismissed. VC 将被弹出(从堆栈顶部删除一个元素,堆栈中的最新元素),而不仅仅是关闭。

In case of presenting scenes modally , these ViewControllers have to have their own NavigationController .在以模态方式呈现场景的情况下,这些 ViewController 必须有自己的NavigationController thus, when dismissing it, you have to write:因此,当解雇它时,你必须写:

dismiss(animated: true, completion: nil)

Do you see the difference?你看得到差别吗? If dismissing is handled properly, then there should be no memory leak.如果解雇处理得当,那么应该没有 memory 泄漏。 Presenting and dismissing is related to Navigation in your app, and every scene has this navigation "section" where you handle dismissing which prevents from memory leak.呈现和关闭与应用程序中的导航有关,每个场景都有这个导航“部分”,您可以在其中处理关闭,以防止 memory 泄漏。

Hope it can help you a little bit.希望它可以帮助你一点点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择到新的视图控制器 - Segue to new view controller 无导航栏的模态搜索到导航控制器 - Modal Segue Into Navigation Controller with No Nav Bar XIB中的导航栏项目,显示了StoryBoard选项卡栏根视图控制器Segue - Nav Bar Item in XIB that Presents a StoryBoard Tab Bar Root View Controller Segue Segue进入另一个没有Nav Bar的视图 - Segue Into another view without Nav Bar 如何在标签栏控制器中从一个视图控制器切换到另一个视图控制器并保留标签栏? - How within a tab bar controller do I segue from one view controller to another and retain the tab bar? segue崩溃到新视图控制器 - Crash on segue to new view controller Swift:如何在视图控制器之间进行切换,并使用导航栏在子视图控制器中向后移动(XLPagerTabStrip) - Swift: How to segue between view controllers and use navigation bar to go backwards within a child view controller (XLPagerTabStrip) 执行segue以查看选项卡和导航控制器后面的控制器 - perform segue to view controller behind tab and nav-controller 在Segue期间同步导航栏以查看控制器 - Sync Navigation Bar To View Controller During Segue 搜索到标签栏和导航栏中的视图控制器 - Segue to a view controller that's within a tabbar and navbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM