转载

UIViewController生命周期分析

做一个实验,通过实验来分析viewController的生命周期。

和生命周期几个相关的方法

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"FirstVC viewDidLoad");

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    NSLog(@"FirstVC viewWillAppear");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"FirstVC didReceiveMemoryWarning");
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    NSLog(@"FirstVC viewWillDisappear");
}
-(void)loadView
{
    [super loadView];
    NSLog(@"FirstVC loadView");
}
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSLog(@"FirstVC viewDidLayoutSubviews");
}
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    NSLog(@"FirstVC viewWillLayoutSubviews");
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    NSLog(@"FirstVC viewDidAppear");
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    NSLog(@"FirstVC viewDidDisappear");
}
-(void)awakeFromNib
{
    [super awakeFromNib];
        NSLog(@"FirstVC awakeFromNib");
}

请注意:为了保证代码顺利执行,且保证模拟器顺利加载ViewController,请务必添加 [super viewxxxxx] 的代码来初始化。 我试过去掉所有的 [super viewxxxx] 代码,控制台打印如下

2016-03-24 10:31:28.328 SIMAlbum[33599:524075] FirstVC awakeFromNib  
2016-03-24 10:31:28.333 SIMAlbum[33599:524075] SecondView awakeFromNib  
2016-03-24 10:31:28.771 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.771 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.834 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.834 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC viewWillAppear  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.835 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.842 SIMAlbum[33599:524075] FirstVC loadView  
2016-03-24 10:31:28.842 SIMAlbum[33599:524075] FirstVC viewDidLoad  
2016-03-24 10:31:28.897 SIMAlbum[33599:524075] FirstVC viewDidAppear
但是你会发现模拟器加载的是一个黑色的没有任何内容的ViewController

相应的在第一个,即将push出来的ViewController也放入上面的代码。

以上便是与viewController生命周期相关的方法。

从StoryBoard加载

先放出进入第一个viewController时的控制台输出

2016-03-24 10:55:17.503 SIMAlbum[35103:546098] FirstVC awakeFromNib  
2016-03-24 10:55:17.506 SIMAlbum[35103:546098] SecondView awakeFromNib  
2016-03-24 10:55:17.625 SIMAlbum[35103:546098] FirstVC loadView  
2016-03-24 10:55:17.626 SIMAlbum[35103:546098] FirstVC viewDidLoad  
2016-03-24 10:55:17.658 SIMAlbum[35103:546098] FirstVC viewWillAppear  
2016-03-24 10:55:17.676 SIMAlbum[35103:546098] FirstVC viewWillLayoutSubviews  
2016-03-24 10:55:17.676 SIMAlbum[35103:546098] FirstVC viewDidLayoutSubviews  
2016-03-24 10:55:17.678 SIMAlbum[35103:546098] FirstVC viewWillLayoutSubviews  
2016-03-24 10:55:17.678 SIMAlbum[35103:546098] FirstVC viewDidLayoutSubviews  
2016-03-24 10:55:17.769 SIMAlbum[35103:546098] FirstVC viewDidAppear

没错,和我们熟悉的生命周期大致内容是一致的。

接下来push进第二个viewController:

2016-03-24 10:55:38.848 SIMAlbum[35103:546098] SecondView awakeFromNib  
2016-03-24 10:55:38.853 SIMAlbum[35103:546098] SecondView loadView  
2016-03-24 10:55:38.865 SIMAlbum[35103:546098] SecondView viewDidLoad  
2016-03-24 10:55:38.865 SIMAlbum[35103:546098] FirstVC viewWillDisappear  
2016-03-24 10:55:38.867 SIMAlbum[35103:546098] SecondView viewWillAppear  
2016-03-24 10:55:38.884 SIMAlbum[35103:546098] SecondView viewWillLayoutSubviews  
2016-03-24 10:55:38.903 SIMAlbum[35103:546098] SecondView viewDidLayoutSubviews  
2016-03-24 10:55:38.904 SIMAlbum[35103:546098] FirstVC viewWillLayoutSubviews  
2016-03-24 10:55:38.904 SIMAlbum[35103:546098] FirstVC viewDidLayoutSubviews  
2016-03-24 10:55:38.905 SIMAlbum[35103:546098] SecondView viewWillLayoutSubviews  
2016-03-24 10:55:38.905 SIMAlbum[35103:546098] SecondView viewDidLayoutSubviews  
2016-03-24 10:55:39.413 SIMAlbum[35103:546098] FirstVC viewDidDisappear  
2016-03-24 10:55:39.413 SIMAlbum[35103:546098] SecondView viewDidAppear

接下来push进第三个ViewController,为了看到第二个viewController的过程,没有在第三个viewController添加任何代码,控制台输出如下:

2016-03-24 10:55:57.906 SIMAlbum[35103:546098] SecondView viewWillDisappear  
2016-03-24 10:55:57.920 SIMAlbum[35103:546098] SecondView viewWillLayoutSubviews  
2016-03-24 10:55:57.920 SIMAlbum[35103:546098] SecondView viewDidLayoutSubviews  
2016-03-24 10:55:58.424 SIMAlbum[35103:546098] SecondView viewDidDisappear

返回第二个viewController:

2016-03-24 10:56:10.539 SIMAlbum[35103:546098] SecondView viewWillAppear  
2016-03-24 10:56:10.541 SIMAlbum[35103:546098] SecondView viewWillLayoutSubviews  
2016-03-24 10:56:10.552 SIMAlbum[35103:546098] SecondView viewDidLayoutSubviews  
2016-03-24 10:56:11.055 SIMAlbum[35103:546098] SecondView viewDidAppear

返回第一个viewController

2016-03-24 10:56:48.577 SIMAlbum[35103:546098] SecondView viewWillDisappear  
2016-03-24 10:56:48.577 SIMAlbum[35103:546098] FirstVC viewWillAppear  
2016-03-24 10:56:48.587 SIMAlbum[35103:546098] FirstVC viewWillLayoutSubviews  
2016-03-24 10:56:48.587 SIMAlbum[35103:546098] FirstVC viewDidLayoutSubviews  
2016-03-24 10:56:49.091 SIMAlbum[35103:546098] SecondView viewDidDisappear  
2016-03-24 10:56:49.091 SIMAlbum[35103:546098] FirstVC viewDidAppear  
2016-03-24 10:56:49.093 SIMAlbum[35103:546098] FirstVC viewWillLayoutSubviews  
2016-03-24 10:56:49.093 SIMAlbum[35103:546098] FirstVC viewDidLayoutSubviews

代码加载viewController

依旧做了个实验,进入代码生成的viewController时控制台输出如下:

2016-03-24 11:09:49.361 SimDraw[36310:564381] FirstVC loadView  
2016-03-24 11:09:49.370 SimDraw[36310:564381] FirstVC viewDidLoad  
2016-03-24 11:09:49.381 SimDraw[36310:564381] FirstVC viewWillAppear  
2016-03-24 11:09:49.393 SimDraw[36310:564381] FirstVC viewWillLayoutSubviews  
2016-03-24 11:09:49.393 SimDraw[36310:564381] FirstVC viewDidLayoutSubviews  
2016-03-24 11:09:49.395 SimDraw[36310:564381] FirstVC viewWillLayoutSubviews  
2016-03-24 11:09:49.395 SimDraw[36310:564381] FirstVC viewDidLayoutSubviews  
2016-03-24 11:09:49.929 SimDraw[36310:564381] FirstVC viewDidAppear

退出时

2016-03-24 11:10:20.636 SimDraw[36310:564381] FirstVC viewWillDisappear  
2016-03-24 11:10:21.166 SimDraw[36310:564381] FirstVC viewDidDisappear

分析与总结

以上的结果简单粗暴,虽然和印象中的一样,但是还是有些许出入,我系统的做了个viewControll的图: UIViewController生命周期分析 注意到其中的viewWillLayoutSubviews和viewDidLayoutSubviews,调用情况视具体的viewDidLoad和viewWillAppear等方法中的代码而定。

viewWillLayoutSubviews调用情况分析

  • init初始化不会触发layoutSubviews
  • addSubview会触发layoutSubviews
  • 设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
  • 滚动一个UIScrollView会触发layoutSubviews
  • 旋转Screen会触发父UIView上的layoutSubviews事件
  • 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
原文  http://xferris.me/uiviewc/
正文到此结束
Loading...