When is UIViewController viewDidUnload called?

前端 未结 5 1523
走了就别回头了
走了就别回头了 2020-11-30 21:26

Note: This question is outdated—viewDidUnload is deprecated iOS 6.

When does UIViewController\'s viewDidUnload automatically get called? Yes I

相关标签:
5条回答
  • 2020-11-30 21:55

    iOS 6.x and later

    I know this is an older question, but I feel an answer should be submitted regarding the changes to the viewDidUnload API in iOS 6 namely that in iOS 6 viewDidUnload is no longer called (at all) and has been deprecated.

    0 讨论(0)
  • 2020-11-30 21:58

    viewDidUnload called in low memory conditions. We should unload stuff that we loaded in viewDidLoad method. We need to relinquish ownership of object by calling accessor method to set it to nil. In case of an outlet, the object release itself so the object reference can be set safely to nil. If not a synthesized property, then we first need to release object than we set to nil.

    0 讨论(0)
  • 2020-11-30 22:02

    If you issue a memory warning in the simulator (look in the menu), this will get called for any view controller attached to a view that is not visible.

    That's because view controllers by default are registered for memory warning notifications, and any view that is not currently being used will be unloaded by the view controller - the viewDidUnload method is there so that you can clean up anything else you would like, to save extra memory (or if you've retained some IBOutlets to help free up memory that would otherwise be released by the view being unloaded).

    Generally any IBOutlets you release in dealloc, should also be released (and references set to nil) in this method.

    0 讨论(0)
  • 2020-11-30 22:03

    -viewDidUnload is called whenever the viewcontroller's view property is set to nil, either manually or most commonly through didReceiveMemoryWarning:.

    0 讨论(0)
  • 2020-11-30 22:07

    In addition to manually issuing a memory warning in the simulator, you can issue one programatically with

    - (void)_simulateLowMemoryWarning {
      // Send out MemoryWarningNotification
      [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
                                                          object:[UIApplication sharedApplication]];
      // Manually call applicationDidReceiveMemoryWarning
      [[[UIApplication sharedApplication] delegate] applicationDidReceiveMemoryWarning:[UIApplication sharedApplication]];
    }
    

    You can then cause this to happen every 5 seconds using a timer

    static NSTimer *gLowMemoryTimer = nil;
    
    - (void)stopLowMemoryTimer {
      [gLowMemoryTimer invalidate];
      gLowMemoryTimer = nil;
    }
    
    - (void)startLowMemoryTimer {
      if (gLowMemoryTimer) {
        [self _stopLowMemoryTimer];
      }
      gLowMemoryTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(_simulateLowMemoryWarning) userInfo:nil repeats:YES];
    }
    
    0 讨论(0)
提交回复
热议问题