Removing a view controller from memory when instantiating a new view controller
You need only to use:
EDIT Swift 4.2
self.dismiss(animated:true, completion: nil)
The rest of work is doing by ARC
To help you during your debug you can add also this code:
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
if let viewControllers = window.rootViewController?.children {
for viewController in viewControllers {
print(viewController.debugDescription)
}
}
}
Remove a modal ViewController from ram SWIFT
Swift uses automatic reference counting for memory management. Objects will be released from memory if no references to them exist anymore.
You can remove a reference by setting a variable referencing the object to nil.
vc = nil
or by using weak references whenever possible.
If you dismiss the view controller, you have to make sure, there are no references left or it will not be released from memory.
Also, iOS may keep some data around. Just make sure, that memory usage does not grow significantly on subsequent presentations of your view controller. Only then you may have a reference cycle.
Remove previous view controllers from view hierarchy after update from background
if let therootController = UIApplication.shared.keyWindow?.rootViewController {
// If rootViewcontroller is navigationController then pop to root if any controllers has been pushed, dismiss if any controllers has been presented.
}
UIApplication.shared.keyWindow?.rootViewController = nil
let rootStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = rootStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController")
rootVC.view.layoutIfNeeded()
UIApplication.shared.keyWindow?.rootViewController = rootVC
UIApplication.shared.keyWindow?.makeKeyAndVisible()
Unable to remove child viewcontroller after Navigation
I added the following in VC1 and it solved my problem
override func viewWillDisappear(_ animated: Bool) {
for controllers in self.childViewControllers
{
controllers.willMove(toParentViewController: nil)
controllers.view.removeFromSuperview()
controllers.removeFromParentViewController()
}
}
Is a ViewController unloaded from memory after being dismissed?
So does this mean that any VC's presented modally are dismissed from memory after you call dismiss
Normally, yes. View controllers form a hierarchy or chain of parent/presenter and child/presented. The former owns and retains the latter, and releases it when the latter is removed. The root controller is never removed unless you deliberately replace it.
Removing the view controller from memory after logging out?
Add NSLog in dealloc
And maybe you have some memory leak in your code(MainViewController).
Related Topics
Swift: Protocol VS. Struct VS. Class
Exc_Bad_Access Using Ibinspectable
Programmatically Set Uipageviewcontroller Transition Style to Scroll
When Should I Use Anyobject Insted of Uibutton in Swift
Cannot Invoke Initializer for Type Unsafemutablepointer<Uint8>
Mpmovieplayercontroller Stops Playing After 5 Seconds - Swift
Most Efficient/Realtime Way to Get Pixel Values from iOS Camera Feed in Swift
Resize Inputaccessoryview Dynamically in iOS 8
Finding It Difficult to Pass Data to Separate Viewcontroller
Alamofireimage Disk Cache Not Working