iOSUIViewController的生命周期

Posted colorful_flowers

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOSUIViewController的生命周期相关的知识,希望对你有一定的参考价值。

-(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
















































































































以上是关于iOSUIViewController的生命周期的主要内容,如果未能解决你的问题,请参考以下文章

React v16.4 的生命周期

React V16.x 生命周期调整

React 学习笔记总结

Vue:基础语法、创建组件、组件间传值、实例生命周期

683 vue3的动态组件,keep-alive,缓存组件的生命周期,异步组件和Suspense,$refs,$parent和$root,生命周期,组件的v-model

vue02-生命周期和基本命令