初步了解一下生命周期的过程:
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
当视图将要从屏幕上移除时候调用
7.viewDidDisappear
当视图已经从屏幕上移除时候调用
8.dealloc
view被销毁时候调用,如果是手动管理内存的话,需要释放掉之前在init和viewDidLoad中分配的内存(类似alloc,new,copy);dealloc方法不能由我们主动调用,必须等引用计数为0时候由系统调用.
注意:viewDidUnload 在6.0之后已经废弃了。简单来说,对于iOS6,你不需要做任何以前viewDidUnload的事情,更不需要把以前viewDidUnload的代码移动到 didReceiveMemoryWarning方法中
二:生命周期图片展示
三;代码示例
首先创建一个UIVIewControl,这页面包含一个button 可以实现跳转到下一个页面;然后查看它的生命周期执行顺序;
- (void)loadView {
[super loadView];
NSLog(@"%@------%s",[self class], __FUNCTION__);
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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__);
} #pragma mark - buttonClick
- (void)barClick:(UIBarButtonItem *)button {
FirstViewController *VC = [[FirstViewController alloc] init];
[self.navigationController pushViewController:VC animated:YES];
}
然后另外建一个视图 :VIewController,
- (void)loadView {
[super loadView];
NSLog(@"%@------%s",[self class], __FUNCTION__);
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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可以转到到FirstViewController 由程序运行结果可知: 当刚运行程序时按顺序依次执行:
1. loadView
2. viewDidLoad
3. viewWillAppear
4. viewDidAppear
5. viewWillDisappear
6. FirstViewController loadView
7. FirstViewController viewDidLoad
8. FirstViewController viewWillAppear
9. viewController viewDidDisappear
10. FirstViewController viewDidappear
当再一次点击左上角的返回键时 从FirstViewController跳转到viewController 由程序的运行结果可知 程序的运行顺序为;
1. loadView
2. viewDidLoad
3. viewWillAppear
4. viewDidAppear
5. viewWillDisappear
6. FirstViewController loadView
7. FirstViewController viewDidLoad
8. FirstViewController viewWillAppear
9. viewController viewDidDisappear
10. FirstViewController viewDidappear
11. FirstViewController viewWillDisappear
12. viewController viewWillAppear
13. FirstViewController viewWillDisappear
14. viewController viewDidAppear
从程序的执行顺序可以看出来每个viewController只执行一次loadView和viewDidLoad