开发者

viewDidUnload called after dealloc?

开发者 https://www.devze.com 2023-02-11 15:06 出处:网络
When I\'m debugging my iPad app, I found because of low memory, some view controllers\' viewDidUnload got called. But seconds later, their viewDidLoad are called. And then again because of low memory,

When I'm debugging my iPad app, I found because of low memory, some view controllers' viewDidUnload got called. But seconds later, their viewDidLoad are called. And then again because of low memory, viewDidUnload, then viewDidLoad again. This is like开发者_开发技巧 file system keep swapping files due to low memory.

Is it supposed to be like this, or I have done something wrong?

Then I wanna release the view controller to get rid of this. But sometimes viewDidUnload is called before dealloc, and then crash due to selector sent to deallocated view controller.

Thank you for any help.


This is perfectly normal behaviour. viewDidUnload is called in a low memory situation to notify your controller that the view has been released.

There's a clear outline of the steps in the View Controller Programming Guide for iOS.

Specially, look at the steps detailed in the section Understanding How Views Are Loaded and Unloaded, where it covers the unload cycle:

  1. The app receives a low-memory warning from the system.

  2. Each view controller calls its didReceiveMemoryWarning method. If you override this method, you should use it to release any memory or objects that your view controller object no longer needs. Do not use it to release your view controller’s view. You must call super at some point in your implementation to ensure that the default implementation runs. The default implementation attempts to release the view.

  3. If the view cannot be safely released (for example, it is visible onscreen), the default implementation returns.

  4. The view controller calls its viewWillUnload method to inform subclasses that the views are about to be removed. A subclass typically overrides the viewWillUnload method when it needs to save any view properties before the views are destroyed.

and so on.

In your specific case, you definitely don't want to release the view controller in this case. If there's any further memory management you want to do whilst there's low memory, you should override the default implementation of didReceiveMemoryWarning. As the docs state, don't forget to call [super didReceiveMemoryWarning];.

0

精彩评论

暂无评论...
验证码 换一张
取 消