本篇文章是对ViewController从出现、产生作用到消失过程的探究,即ViewController的生命周期。
ViewController
是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,管理着应用中的众多视图。 iOS的SDK中提供很多原生ViewController,以支持标准的用户界面,如表视图控制器(UITableViewController
)、导航控制器(UINavigationController
)、标签栏控制器(UITabbarController
)等。
按结构可以对iOS的所有ViewController分成两类:
UITableViewController
,UIViewController
。UINavigationController
,UITabbarController
。它们都有一个属性:viewControllers。其中UINavigationController表示一种Stack式结构,push或pop一个ViewController,后一个ViewController会依赖前一个ViewController。而UITabbarController表示一个Array结构,各个ViewController是并列的。- (void) awakeFromNib
view如果是由xib创建,它会执行awakeFromNib方法,此方法是当xib或者storyboard加载完之后调用,此时nib文件中已经连线好的对象已经完成初始化,frame位置已固定。
- (void) loadView
用于创建Controller的View。每次访问controller的view(比如controller.view、self.view)且view为nil,loadView方法就会被调用。
loadView在ViewController的生命周期中一般情况只会被调用一次。
- (void) viewDidLoad
在视图控制器将视图层次结构加载到内存之后调用,无论视图层次结构是从nib文件中加载还是从loadView方法里用代码实现都会执行此方法。如果是在代码中创建的视图加载器,他将会在loadView方法后被调用;如果是从nib视图页面输出,他将会在视图设置好后后被调用。
- (void) viewWillAppear:(BOOL)animated
通知视图控制器其视图即将添加到视图层次结构中。
- (void) viewWillLayoutSubViews
通知视图控制器其视图将要布局它的子视图。
- (void) viewDidLayoutSubViews
通知视图控制器其视图已将其子视图布局好。
- (void) viewDidApper:(BOOL)animated
通知视图控制器其视图已经添加到视图层次结构中。
- (void) viewWillDisappear:(BOOL)animated
通知视图控制器其视图将要从视图层次结构中删除。
- (void) viewDidDisappear:(BOOL)animated
通知视图控制器其视图已经从视图层次结构中删除。
- (void) didReceiveMemoryWarning
当应用收到内存警告时会调用此方法。
demo创建了两个ViewController:ViewController和SecondViewController。在ViewController中可以present到SecondViewController,在SecondViewController中可以dismiss到ViewController。
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *presentButton = [UIButton buttonWithType:UIButtonTypeSystem];
presentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[presentButton addTarget:self action:@selector(present:) forControlEvents:UIControlEventTouchUpInside];
[presentButton setTitle:@"跳转" forState:UIControlStateNormal];
presentButton.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:presentButton];
}
- (void)present:(UIButton *)button {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:secondViewController animated:YES completion:nil];
}
@end
//SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(nonatomic, strong)UIButton *dismissButton;
@end
//SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
#pragma mark init
- (instancetype)init{
self = [super init];
if(self){
NSLog(@"%s",__func__);
}
return self;
}
#pragma mark loadView
- (void)loadView {
[super loadView];
NSLog(@"%s",__func__);
}
#pragma mark viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_dismissButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
[_dismissButton setTitle:@"返回" forState:UIControlStateNormal];
_dismissButton.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:_dismissButton];
NSLog(@"%s",__func__);
}
#pragma mark viewWillLayoutSubviews
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"%s",__func__);
}
#pragma mark viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"%s",__func__);
}
#pragma mark viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%s",__func__);
}
#pragma mark viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%s",__func__);
}
#pragma mark viewWillDisappear:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%s",__func__);
}
#pragma mark viewDidDisappear:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"%s",__func__);
}
#pragma mark didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
NSLog(@"%s",__func__);
}
- (void)dismiss:(UIButton*)button {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end