Correct way to remove (remove) view / viewController from stack after segue in Swift
I have a problem. I present performances performSegueWithIdentifier
. Everything is going smoothly. The problem is that as a test I only have 2 viewControllers with some data in them, and I have two buttons that cause switching to another VC. If I continue to execute the functions, you can clearly see that the memory usage is increasing every two segments by about 0.4 MB. This tells me that views are not popped / removed from the view stack and are just using memory. I would like to know the correct way to get rid of the view representing another view using performSegueWithIdentifier
(of course after it has finished the view of the view, otherwise it won't work, I guess).
Could you point me in the right direction? I found objective-c code that tried to do this, but it was very extensive and I don't know much about objective-C, so it was a little difficult for me to understand.
Thank you in advance for your help!
Hooray!
Edit:
I am doing a "manual session". By that, I mean I have two view controllers standing on their own. They are not built into any navigation systems or anything like that. I am using "Adaptive Segue" of type "Show".
source to share
If your goal is to always keep one VC, and since you already have a manual session, then what can you do, after submitting the next VC, you can pull the existing VC from the root and assign the target VC as your VC root like so
class ManualSegue: UIStoryboardSegue {
override func perform() {
sourceViewController.presentViewController(destinationViewController, animated: true) {
self.sourceViewController.navigationController?.popToRootViewControllerAnimated(false)
UIApplication.sharedApplication().delegate?.window??.rootViewController = self.destinationViewController
}
}
}
source to share
When you run regular sessions, you accumulate new view controllers on top of existing ones without releasing them. This is why your memory usage continues to grow.
You need to provide additional information before we can give you specific recommendations. Are you doing modal segments or pushing the navigation stack?
If you click, you want to jump out to the view controller you came from. Think of a stack of plates. When you push, you add the plates to the stack. When you show up, you shoot the tablets and show the tablets (view controllers) at the bottom. Search the Xcode Help under "PopTo" to find methods that allow you to either go back to a specific view controller or go back to the root view controller of your navigation controller.
Modular view controllers are stacked on top of each other in a similar way, but without creating a navigation controller. If you hit a series of one or more modals and you send a method dismissViewControllerAnimated:completion:
to the view controller you want to return to, it will reject the modals that are on top of it.
You can also see how to use Unwind Segments. Untie the passages so you go back to where you came from. For more information, search the Xcode help under "Using Unwinding Sections". Most of the segues unwinding tutorials are written in Objective-C, but you can find them in Swift if you take a look. Explain unwinding snippets in enough detail to be helpful, more than I can cover in this answer.
source to share