既然要研究UIViewController的生命周期, 先写一段代码, 我们从代码的执行顺序来了解UIViewController的生命周期
首先 要知道一个UIViewController中都会执行那些方法
1. - (void)loadView
2. - (void)viewDidLoad
3. - (void)viewWillAppear:(BOOL)animated
4. - (void)viewDidAppear:(BOOL)animated
5. - (void)viewWillDisappear:(BOOL)animated
6. - (void)viewDidDisappear:(BOOL)animated
在一段程序执行过程中 按以上顺序依次执行
下面 我们书写代码 运行程序 来看看 首先创建一个 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 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__);
}
由图可知程序的运行顺序是:
loadView
viewDidLoad
viewWillAppear
-
viewDidAppear
这是只有一个页面的情况 如果在这个页面上放一个button push到下一个页面 这是程序执行顺序又会是怎么样的呢
下面让我们再看看程序的运行结果- (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]; } - (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);
}
- (void)viewWillAppear:(BOOL)animated {
在ViewController页面中有一个button 点击button可以push到FirstViewController 由程序运行结果可知: 当刚运行程序时按顺序依次执行:
- loadView
- viewDidLoad
- viewWillAppear
- viewDidAppear
- viewWillDisappear
- FirstViewController loadView
- FirstViewController viewDidLoad
- FirstViewController viewWillAppear
- viewController viewDidDisappear
- FirstViewController viewDidappear
当再一次点击左上角的返回键时 从FirstViewController跳转到viewController 由程序的运行结果可知 程序的运行顺序为;
- loadView
- viewDidLoad
- viewWillAppear
- viewDidAppear
- viewWillDisappear
- FirstViewController loadView
- FirstViewController viewDidLoad
- FirstViewController viewWillAppear
- viewController viewDidDisappear
- FirstViewController viewDidappear
- FirstViewController viewWillDisappear
- viewController viewWillAppear
- FirstViewController viewWillDisappear
-
viewController viewDidAppear
从程序的执行顺序可以看出来每个viewController只执行一次loadView和viewDidLoad