Uiviewcontroller Prevent View from Unloading

UIViewController prevent view from unloading

What appears to be working for me was to override setView: to ignore setting to nil. It's kludgy, but then, this is a kludgy issue, and this did the trick:

-(void)setView:(UIView*)view {
if(view != nil || self.okayToUnloadView) {
[super setView:view];
}
}

How to unload self.view from UIViewController in Swift

setValue(nil, forKey:"view") seems to work:

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if self.view.window == nil {
self.setValue(nil, forKey: "view")
}
}

What is the proper way to unload views in iOS 6 in a memory warning (Apple doc flaw)?

The correct check in a view controller for the view being loaded and on screen is:

if ([self isViewLoaded] && [self.view window] == nil)

My full solution in iOS 6 to have a view controller unload views and cleanup similar to iOS 5 is the following:

// will not be called in iOS 6, see iOS docs
- (void)viewWillUnload
{
[super viewWillUnload];
[self my_viewWillUnload];
}

// will not be called in iOS 6, see iOS docs
- (void)viewDidUnload
{
[super viewDidUnload];
[self my_viewDidUnload];
}

// in iOS 6, view is no longer unloaded so do it manually
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && [self.view window] == nil) {
[self my_viewWillUnload];
self.view = nil;
[self my_viewDidUnload];
}
}

- (void)my_viewWillUnload
{
// prepare to unload view
}

- (void)my_viewDidUnload
{
// the view is unloaded, clean up as normal
}

How to prevent page from unloading when app is active

You should not prevent view unloading, if the application runs out of memory, it MUST free memory, or else your app will simply be killed by the system. You should really cleanup as much memory as you can, as well as views.

Also, views are only there to display data, if the view is unloaded it's only in one specific case: there was a memory warning and the view didn't have a superview (not visible to the user). If it's not visible to the user, it makes absolutely no sense to keep it around when running out of memory. If you're storing [important] data in these views, you're doing it wrong. Data model should be kept in controllers.

stop view from disappearing

Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.

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)
}
}
}


Related Topics



Leave a reply



Submit