一:首先了解一下生命周期图
二:UIViewController 生命周期介绍
1.通过alloc init 分配内存,初始化controller;
2.loadView, loadView方法默认实现[super loadView]; 如果在初始化controller时指定了xib文件名,就会根据传入的xib文件名加载对应的xib文件,如果没传xib文件名,默认会加载跟controller同名的xib文件,如果没找到相关联的xib文件,就会创建一个空白的UIView,然后赋給controller的view;
3.viewDidLoad, 当loadView创建完view之后,此时view已经完成加载了,会调用viewDidLoad方法;一般我会在这里做界面上的初始化操作,比如添加按钮,子视图,等等;
4.viewWillAppear, 当view在load完之后,将要显示在屏幕之前会调用这个方法, 在重写这些方法时候最好先调用一下系统的方法之后在做操作;
5.viewDidAppear , 当view已经在屏幕上显示出来之后,会调用这个方法;
6.viewWillDisappear, 当view视图将要从屏幕上移除时候调用;
7.viewDidDisappear, 当视图已经从屏幕上移除时候调用;
8.dealloc, view被销毁时候调用,如果是手动管理内存的话,需要释放掉之前在init和viewDidLoad中分配的内存(类似alloc,new,copy);dealloc方法不能由我们主动调用,必须等引用计数为0时候由系统调用.
注意:viewDidUnload 在6.0之后已经废弃了。简单来说,对于iOS6,你不需要做任何以前viewDidUnload的事情,更不需要把以前viewDidUnload的代码移动到 didReceiveMemoryWarning方法中
三:UIViewController 生命周期实例
首先创建一个 ViewController,并这个页面上放一个button push到下一个页面;查看它的生命周期执行顺序
ViewController.m中的代码:
- (void)loadView {
[super loadView];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@------%s",[self class], __FUNCTION__);
self.view.backgroundColor = [UIColor redColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barClick:)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
#pragmamark - buttonClick
- (void)barClick:(UIBarButtonItem *)button {
SecondViewController *VC = [[FirstViewController alloc] init];
[self.navigationController pushViewController:VC animated:YES];
}
第二个控制器SecondViewController.m 代码
- (void)loadView {
[super loadView];
NSLog(@"%@------%s",[selfclass], __FUNCTION__);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@------%s",[self class], __FUNCTION__);
self.view.backgroundColor = [UIColor greenColor];
}
// 视图将要出现时就会调用
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
// 视图完全出现时就会调用
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
// 视图将要消失会调用
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
// 视图完全消失会调用
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
在ViewController页面中有一个button 点击button可以push到SecondViewController 由程序运行结果可知: 当刚运行程序时按顺序依次执行:
1. viewController loadView
2. viewController viewDidLoad
3. viewController viewWillAppear
4. viewController viewDidAppear
5. viewController viewWillDisappear
6. SecondViewController loadView
7. SecondViewController viewDidLoad
8. SecondViewController viewWillAppear
9. viewController viewDidDisappear
10. SecondViewController viewDidappear