码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS】UIViewController的生命周期

时间:2018-06-15 13:14:06      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:log   编码   lap   上下级   rbo   TE   elf   nss   nim   

-(void)loadView;

  • loadView方法在UIVIewController的view被访问且为空的时候调用。
  • loadView方法不应该直接被调用,而是由系统调用。它会加载或创建一个View并把它赋值给UIViewController的view属性。
    以下代码会导致死循环

-(void)loadView{
NSLog(@"ViewController loadView");
}

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"ViewController viewDidLoad");
    self.view.backgroundColor = [UIColor whiteColor];
    }

- (void)viewDidLoad;

  • 当loadView将view加载完成,会调用viewDidLoad方法,该方法在UIVIewController初始化过程中只会执行一次,在该方法中可以进行页面相关的初始化。

    -(void)viewWillAppear:(BOOL)animated;

  • 页面将要出现时调用。页面每次即将出现时都会调用。
  • 当app有多级ViewController,pop操作后都会调用即将出现的ViewController的该方法,如果每次ViewController的出现都需要刷新页面,可在该方法中实现。

    -(void)viewWillLayoutSubviews;

  • ViewController的view即将对subviews进行布局。

    -(void)viewDidLayoutSubviews;

  • ViewController的view对subviews的布局完成。

    -(void)viewDidAppear:(BOOL)animated;

  • 页面已经出现时调用。页面每次出现时都会调用。
  • 在view被添加到视图层级中以及多视图,上下级视图切换时调用这个方法,在这里可以对正在显示的视图做进一步的设置。

    -(void)viewWillDisappear:(BOOL)animated;

  • 页面将要消失时调用。可能会被多次调用。
  • 页面之间push或者pop时都会调用。

    -(void)viewDidDisappear:(BOOL)animated;

  • 页面已经消失时调用。可能会被多次调用。
  • 页面之间push或者pop时都会调用。

    -(void)dealloc;

  • 页面被销毁时调用。
  • 该方法可以用来调试页面是否存在内存泄漏,如果存在,在页面出栈时将不会调用该方法。
  • 在该方法中可以处理需要手动释放的东西,比如观察者的移除。

    常用周期方法执行顺序

    由于我编码习惯是纯代码布局,所以对xib布局和storyboard布局的相关方法未加研究,代码如下

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"ViewController viewDidLoad");
    self.view.backgroundColor = [UIColor whiteColor];
    }

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"ViewController viewWillAppear");
}

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"ViewController viewDidAppear");
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"ViewController viewWillDisappear");
}

-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"ViewController viewDidDisappear");
}

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(@"ViewController viewWillLayoutSubviews");
}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(@"ViewController viewDidLayoutSubviews");
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • 打印结果:

ViewController viewDidLoad
ViewController viewWillAppear
ViewController viewWillLayoutSubviews
ViewController viewDidLayoutSubviews
ViewController viewDidAppear

以上只是显示一个页面的代码和打印结果,下面我们来看一下页面间的跳转时,生命周期执行的顺序。
从A页面调转到B页面,A页面代码:

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"A viewDidLoad");
    self.view.backgroundColor = [UIColor whiteColor];
    }

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"A viewWillAppear");
}

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"A viewDidAppear");
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"A viewWillDisappear");
}

-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"A viewDidDisappear");
}

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(@"A viewWillLayoutSubviews");
}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(@"A viewDidLayoutSubviews");
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(void)touchesBegan:(NSSet

B页面代码:

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"B viewDidLoad");
    self.view.backgroundColor = [UIColor whiteColor];
    }

-(void)dealloc{
NSLog(@"B dealloc");
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"B viewWillAppear");
}

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"B viewDidAppear");
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"B viewWillDisappear");
}

-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"B viewDidDisappear");
}

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(@"B viewWillLayoutSubviews");
}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(@"B viewDidLayoutSubviews");
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(void)touchesBegan:(NSSet

}

从A页面跳转到B页面,再从B页面回到A页面,打印结果:

A viewDidLoad
A viewWillAppear
A viewWillLayoutSubviews
A viewDidLayoutSubviews
A viewDidAppear
************************跳转啦****************************
B viewDidLoad
A viewWillDisappear
B viewWillAppear
B viewWillLayoutSubviews
B viewDidLayoutSubviews
A viewDidDisappear
B viewDidAppear
************************回去啦****************************
B viewWillDisappear
A viewWillAppear
B viewDidDisappear
A viewDidAppear
B dealloc

【iOS】UIViewController的生命周期

标签:log   编码   lap   上下级   rbo   TE   elf   nss   nim   

原文地址:https://www.cnblogs.com/zhanqin/p/9186379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!