init方法
- 在init方法中实例化必要的对象(遵从LazyLoad思想)
- init方法中初始化ViewController本身
loadView方法
- 当view需要被展示而它却是nil时,viewController会调用该方法。不要直接调用该方法。
- 如果手工维护views,必须重载重写该方法
- 如果使用IB维护views,必须不能重载重写该方法
- loadView和IB构建view
viewDidLoad方法
- 重载重写该方法以进一步定制view
- 在iPhone OS 3.0及之后的版本中,还应该重载重写viewDidUnload来释放对view的任何索引
- viewDidLoad后调用数据Model
viewDidUnload方法
- 当系统内存吃紧的时候会调用该方法(注:viewController没有被dealloc)
- 内存吃紧时,在iPhone OS 3.0之前didReceiveMemoryWarning是释放无用内存的唯一方式,但是OS 3.0及以后viewDidUnload方法是更好的方式
- 在该方法中将所有IBOutlet(无论是property还是实例变量)置为nil(系统release view时已经将其release掉了)
- 在该方法中释放其他与view有关的对象、其他在运行时创建(但非系统必须)的对象、在viewDidLoad中被创建的对象、缓存数据等
- release对象后,将对象置为nil(IBOutlet只需要将其置为nil,系统release view时已经将其release掉了)
- 一般认为viewDidUnload是viewDidLoad的镜像,因为当view被重新请求时,viewDidLoad还会重新被执行
- viewDidUnload中被release的对象必须是很容易被重新创建的对象(比如在viewDidLoad或其他方法中创建的对象),不要release用户数据或其他很难被重新创建的对象
dealloc方法
- viewDidUnload和dealloc方法没有关联,dealloc还是继续做它该做的事情
举例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/
*
*
The
view
hierarchy
for
this controller has been torn
down
. This usually happens
in
response
to
low memory notifications.
*
All IBOutlets should be released
by
setting their
property
to
nil
in
order
to
free
up
as
much memory
as
possible.
*
This
is
also a good place
to
release other variables
that
can be recreated when needed.
*
/
-
(
void
)
viewDidUnload
{
self.startButton
=
nil;
[setupViewController release];
setupViewController
=
nil;
}
-
(
void
)
dealloc
{
[startButton release];
[setupViewController release];
[super dealloc];
|
文章出处:http://blog.163.com/zyc-to/blog/static/17152400201011299129231/