理解view的生命周期:
在UIViewController中,view(黑体的view指的是controller的view属性)有两个循环:加载和卸载循环。当程序的一部分向controller请求view的指针且view不在内存中时,view会进入加载循环,controller会将view加载入内存。
当程序接收到内存警告时,controller会尝试卸载view,在卸载循环中,controller尝试释放它的view对象并返回到原始的无view状态(当它不在屏幕上显示时,这个条件的判断到底是根据view的结构来还是根据用户视觉来,我尚不清楚),直到view下次被请求。
在加载卸载循环中,controller处理的大部分逻辑。但是如果我们的controller还“持有”着view的后代view时,或者还有其他后续操作需要进行时,我们可以重载特定函数(后面会介绍到)来另行处理。
加载循环:
-
程序请求了controller的view.
-
如果view当前不在内存中,controller调用loadview函数。
-
loadView
进行如下操作: -
controller 调用
viewDidLoad
方法来执行一些加载时(加载时一词,相对于编译时、运行时)任务.
程序可以重载loadView
和 viewDidLoad来执行一些任务:
Figure 2-2 将view载入内存
卸载循环:
-
程序收到内存警告.
-
每个view controller调用
didReceiveMemoryWarning
:-
If you override this method, you should use it to release any custom data that your view controller object no longer needs. You should not use it to release your view controller’s view. You must call
super
at some point in your implementation to perform the default behavior.(iOS3.0以后不建议重载这个函数来进行额外的清除操作,使用viewDidUnload) -
默认实现会在确定可以安全地释放view时释放掉view。
-
-
如果controller释放了它的view, 它会调用
viewDidUnload
. .可以重载这个函数来进行额外的清理操作(不要清除view和那些加载循环中无法rebuild的数据)。
Figure 2-3 卸载循环
Important: In iOS 3.0 and later, the
viewDidUnload
method is the preferred place to put any code related to cleaning up your views. You might also override thedidReceiveMemoryWarning
method to release temporary caches or other private data that is no longer needed when the view is released. If you do overridedidReceiveMemoryWarning
, always callsuper
to give the inherited version of the method a chance to release the view.In iOS 2.2 and earlier, you must use the
didReceiveMemoryWarning
method to perform your view-related cleanup and to release any unneeded private data structures. TheviewDidUnload
method is available only in iOS 3.0 and later.For more information about managing memory during low-memory conditions, see
看看viewDidUnload的函数文档:
viewDidUnload
Called when the controller’s view is released from memory.
- (void)viewDidUnload
Discussion
When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup. If your view controller stores references to the view or its subviews, you should use this method to release those references (if you retained the objects initially) and set those references to nil. You can also use this method to release any objects that you created to support the view but that are no longer needed now that the view is gone. You should not use this method to release user data or any other information that cannot be easily recreated.
Typically, a view controller stores references to objects using an outlet, which is a variable or property that includes the IBOutlet keyword and is configured using Interface Builder. A view controller may also store pointers to objects that it creates programmatically, such as in the viewDidLoad method. The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly. For more information about memory management practices, see Advanced Memory Management Programming Guide.
By the time this method is called, the view property is nil.
Special Considerations
If your view controller stores references to views and other custom objects, it is also responsible for relinquishing ownership of those objects safely in its dealloc method. If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.