文章目录
- 一、生命周期
- 二、注意事项
- 案例讲解
一、生命周期
viewController
有自己的生命周期,其生命周期如下图:
init
- 初始化程序;loadView
- 在UIViewController
对象的view
被访问且为空的时候调用;viewDidLoad
- 视图加载完成后调用;viewWillAppear
-UIViewController
对象的视图即将加入窗口时调用;viewDidAppear
-UIViewController
对象的视图已经加入窗口时调用;viewWillDisappear
-UIViewController
对象的视图即将消失时调用;viewDidDisappear
-UIViewController
对象的视图已经消失时调用;didReceiveMemoryWarning
- 出现内存警告;dealloc
- 视图被销毁,此次需要在init
和viewDidLoad
中创建的对象进行释放;
初始化
init
方法:初始化UIViewController
,执行关键数据初始化操作。不要在此操作view,view在loadView
: 方法中才初始化。
加载视图
loadView
: 方法是在UIViewController
对象的view被访问且为空的时候调用,这个方法会加载或者创建一个view并将其赋值给view属性。在ViewController的生命周期中没有特殊情况只会被调用一次。
你可以通过重写该方法来手动初始化views。在该方法的实现中,不能调用super。viewDidLoad
方法:view加载完成后调用。通常,对于各种初始化数据的载入,初始设定、修改约束、移除视图等很多操作都可以这个方法中实现。通常在此方法中添加一些控件和视图。viewWillAppear
方法:系统在载入所有的数据后,将会在屏幕上显示视图,这时会先调用这个方法,通常会在这个方法对即将显示的视图做进一步的设置。比如,设置设备不同方向时该如何显示;设置状态栏方向、设置视图显示样式等。
当APP有多个视图时,上下级视图切换是也会调用这个方法,如果在调入视图时,需要对数据做更新,就只能在这个方法内实现。viewDidAppear
方法:UIViewController对象的视图已经加入窗口时调用。这时可以对正在显示的视图做设置。viewWillDisAppear
方法:UIViewController
对象的视图即将消失、被覆盖或是隐藏时调用。viewDidDisAppear
方法:UIViewController
对象的视图已经消失、被覆盖或是隐藏时调用。
内存警告
didReceiveMemoryWarning
方法:在内存足够的情况下,app视图通常会一直保存在内存中,如果内存不够,会释放掉自己拥有的视图。系统只会释放内存,不会释放对象的所有权,通常需要在这里将不需要显示在内存中保留的对象释放它的所有权,置为nil。
视图销毁
dealloc
方法:销毁视图,释放资源。不要手动调用此方法,当引用计数值为0的时候,系统会自动调用此方法。
二、注意事项
loadView
和viewDidLoad
的区别就是,loadView
时view还没有生成,viewDidLoad
时,view已经生成了,loadView
只会被调用一次,而viewDidLoad
可能会被调用多次(View可能会被多次加载),当view被添加到其他view中之前,会调用viewWillAppear
,之后会调用viewDidAppear
。当view从其他view中移除之前,调用viewWillDisAppear
,移除之后会调用viewDidDisappear
。当view不再使用时,受到内存警告时,ViewController
会将view释放并将其指向为nil
。
案例讲解
分为A和B两个视图。
A视图:
AViewController.h 文件:
#import <UIKit/UIKit.h>
#import "BViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface AViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
AViewController.m文件:
#import "AViewController.h"
@interface AViewController ()
@end
@implementation AViewController
- (void) loadView {
[super loadView];
NSLog(@"AViewController loadView");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 50);
button.center = self.view.center;
[button setTitle:@"按键" forState:UIControlStateNormal];
[button addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
NSLog(@"AViewController viewDidLoad");
}
- (void) viewWillAppear:(BOOL)animated {
NSLog(@"AViewController viewWillAppear");
}
- (void) viewDidAppear:(BOOL)animated {
NSLog(@"AViewController viewDidAppear");
}
- (void) viewWillDisappear:(BOOL)animated {
NSLog(@"AViewController viewWillDisappear");
}
- (void) viewDidDisappear:(BOOL)animated {
NSLog(@"AViewController viewDidDisappear");
}
- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"AViewController didReceiveMemoryWarning");
}
- (void) press:(UIButton*)sender {
BViewController* bViewController = [[BViewController alloc] init];
bViewController.modalPresentationStyle = UIModalPresentationFullScreen;
NSLog(@"A->B");
[self presentViewController:bViewController animated:YES completion:nil];
}
@end
BViewController.h文件:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
BViewController.m文件:
#import "BViewController.h"
@interface BViewController ()
@end
@implementation BViewController
- (void) loadView {
[super loadView];
NSLog(@"BViewController loadView");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 50);
button.center = self.view.center;
[button setTitle:@"按键" forState:UIControlStateNormal];
[button addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
NSLog(@"BViewController viewDidLoad");
}
- (void) viewWillAppear:(BOOL)animated {
NSLog(@"BViewController viewWillAppear");
}
- (void) viewDidAppear:(BOOL)animated {
NSLog(@"BViewController viewDidAppear");
}
- (void) viewWillDisappear:(BOOL)animated {
NSLog(@"BViewController viewWillDisappear");
}
- (void) viewDidDisappear:(BOOL)animated {
NSLog(@"BViewController viewDidDisappear");
}
- (void) press:(UIButton*)sender {
NSLog(@"B->A");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
运行结果:
2022-09-18 18:34:13.549033+0800 ViewController[25007:24031989] AViewController loadView
2022-09-18 18:34:13.554447+0800 ViewController[25007:24031989] AViewController viewDidLoad
2022-09-18 18:34:13.563963+0800 ViewController[25007:24031989] AViewController viewWillAppear
2022-09-18 18:34:13.697255+0800 ViewController[25007:24031989] AViewController viewDidAppear
2022-09-18 18:34:16.421472+0800 ViewController[25007:24031989] A->B
2022-09-18 18:34:16.422527+0800 ViewController[25007:24031989] BViewController loadView
2022-09-18 18:34:16.423160+0800 ViewController[25007:24031989] BViewController viewDidLoad
2022-09-18 18:34:16.428482+0800 ViewController[25007:24031989] AViewController viewWillDisappear
2022-09-18 18:34:16.428732+0800 ViewController[25007:24031989] BViewController viewWillAppear
2022-09-18 18:34:16.933891+0800 ViewController[25007:24031989] BViewController viewDidAppear
2022-09-18 18:34:16.934169+0800 ViewController[25007:24031989] AViewController viewDidDisappear
2022-09-18 18:34:31.586055+0800 ViewController[25007:24031989] B->A
2022-09-18 18:34:31.587979+0800 ViewController[25007:24031989] BViewController viewWillDisappear
2022-09-18 18:34:31.588890+0800 ViewController[25007:24031989] AViewController viewWillAppear
2022-09-18 18:34:32.093543+0800 ViewController[25007:24031989] AViewController viewDidAppear
2022-09-18 18:34:32.093834+0800 ViewController[25007:24031989] BViewController viewDidDisappear