一 生命周期图
UIViewController生命周期介绍
- 通过alloc init 分配内存,初始化controller.
在init里应该只有相关数据的初始化,而且这些数据都是比较关键的数据。init里不要掉self.view,否则会导致viewcontroller创建view。 - loadview中这个方法中,要正式加载View.(是一个方法)
负责创建UIViewController的view ,如果我们没有重载这个方法。它会调用[super loadView];返回一个View .
一般用于创建比较关键的tableViewController和tableView,UINavigationController的navgationBar,不可掉用view的getter(在掉super loadView前)在get方法里,会判断view是否已经存在,存在直接返回,不存在调用loadview. - viewDidLoad 当loadview创建完view后,此时view已经完成加载,会调用viewDidLoad方法。
一般在这里做界面上的初始化操作,比如添加按钮,子视图。
- load view 和viewDidLoad 区别
在 loadView 之前,是没有 view 的,也就是说,在这之前,view 还没有被初始化。完成 viewDidLoad 方法后,ViewController 里面就成功的加载 view了
loadView只会被调用一次,而viewDidLoad可能会被调用多次(View可能会被多次加载).
- viewWillAppear,当view在load完之后,将要显示在屏幕之前会调用这个方法,在view被添加到superview之前,切换动画之前调用。在这里可以进行一些显示前的处理。比如键盘弹出,一些特殊的过程动画(比如状态条和navigationbar颜色)。
- viewDidAppear,当view已经在屏幕上显示出来
一般用于显示后,在切换动画后,如果有需要的操作,可以在这里加入相关代码。 - viewWillDisappear,当视图将要从屏幕中移除时调用
- viewDidDisappear,当视图已经从屏幕上移除时调用
- viewWillLayoutSubviews: 控制器的view将要布局子控件(在这个方法里,部署需要改变重新刷新view的代码,功能类似viewlayoutSubViews()这个方法,需要注意的是,这个方法里一般都需要重置的view的frame,宽度和高度的获取,因此view的frame一般都写在这个方法里)
- viewDidLayoutSubviews: 控制器的view布局子控件完成
- dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放
- viewDidUnload: 卸载view。
代码显示
viewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"viewDidLoad");
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:btn];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:@"按钮" forState:UIControlStateNormal];
btn.frame = CGRectMake(200, 200, 100, 40);
[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
}
//gray当view调用完load之后,在显示屏幕之前调用这个方法
- (void) viewWillAppear:(BOOL)animated
{
_gray = [[UIView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_gray];
_gray.backgroundColor = [UIColor yellowColor];
NSLog(@"graywillAppear");
}
//blue 在view已经在屏幕上显现之后,调用这个方法
- (void) viewDidAppear:(BOOL)animated
{
_blue = [[UIView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_blue];
_blue.backgroundColor = [UIColor blueColor];
NSLog(@"blueViewDidAppear");
}
//red 当view将要从屏幕上移除时
- (void)viewWillDisappear:(BOOL)animated {
_red = [[UIView alloc] initWithFrame:CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
[self.view addSubview:_red];
_red.backgroundColor = [UIColor redColor];
NSLog(@"redViewWillDisappear");
}
//orange 当view已经从屏幕上移除时
- (void) viewDidDisappear:(BOOL)animated
{
_orange = [[UIView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_orange];
_orange.backgroundColor = [UIColor orangeColor];
NSLog(@"orangeDidDisappear");
}
- (void)viewDidLayoutSubviews {
_purper = [[UIView alloc] initWithFrame:CGRectMake(0,300, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_purper];
_purper.backgroundColor = [UIColor purpleColor];
NSLog(@"purpleviewDidLayoutSubviews");
}
- (void)viewWillLayoutSubviews{
_yellow =[[UIView alloc] initWithFrame:CGRectMake(0,300, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_yellow];
_yellow.backgroundColor = [UIColor yellowColor];
NSLog(@"yellowviewWillLayoutSubviews");
}
- (void) loadView{
[super loadView];//要调用父类
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 99, 99)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
NSLog(@"loadview");
}
-(void) press
{
NSLog(@"------ press button ------");
SecondViewController *second = [[SecondViewController alloc]init];
second.view.backgroundColor = [UIColor yellowColor];
second.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:second animated:NO completion:nil];
}
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 200, 100, 40);
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(press2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"view2willappear");
}
- (void)viewWillDisappear:(BOOL)animated{
NSLog(@"view2willDisAppear");
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"view2alreadyappear");
}
- (void)viewDidDisappear:(BOOL)animated{
NSLog(@"view2alreadydisappear");
}
- (void) press2{
NSLog(@"------- press back -------");
[self dismissViewControllerAnimated:NO completion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
结果