When is UIViewController viewDidUnload called?

前端 未结 5 1536
走了就别回头了
走了就别回头了 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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];
    }
    

提交回复
热议问题