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

前端 未结 1 564
谎友^
谎友^ 2020-12-24 09:19

In iOS 6, viewWillUnload and viewDidUnload are deprecated and UIViewControllers no longer unload views that are not visible on screen during a memo

相关标签:
1条回答
  • 2020-12-24 09:39

    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
    }
    
    0 讨论(0)
提交回复
热议问题