UIView与UIViewController的生命周期

iOS开发中,创建View常见的两种方式:一个是纯代码,一个是借助于XIB;
创建ViewController常见的也有两种方式:一个是纯代码,一个是借助于StoryBoard。

重写UIView的方法如下:

@implementation CustomView

-(instancetype)init{
    
    self = [super init];
    
    NSLog(@"%s", __func__);
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:(CGRect)frame];
    
    NSLog(@"%s", __func__);
    
    return self;
    
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    
    NSLog(@"%s", __func__);
    
    return self;   
}

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    NSLog(@"%s", __func__);
}

-(void)layoutSubviews{
    
    NSLog(@"%s", __func__);
    
}

@end

结论总结

  1. 如果纯代码创建UIView,调用方式为CustomView *view = [[CustomView alloc]init];那么它的生命周期为:
-[CustomView initWithFrame:]
-[CustomView init]
-[CustomView layoutSubviews]

  1. 如果纯代码创建UIView,调用方式为CustomView *view = [[CustomView alloc]initWithFrame:[UIScreen mainScreen].bounds];那么它的生命周期为:
-[CustomView initWithFrame:]
-[CustomView layoutSubviews]

  1. 如果通过XIB创建UIView,调用方式为NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:nil options:nil];[array lastObject];那么它的生命周期为:
-[CustomView initWithCoder:]
-[CustomView awakeFromNib]
-[CustomView layoutSubviews]

重写ViewController的如下方法:

@implementation CustomViewController

-(void)loadView{
    
    NSLog(@"%s", __func__);
    
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"%s", __func__);
    
    self.view.backgroundColor = [UIColor redColor];
}


-(void)viewDidAppear:(BOOL)animated{
    
    NSLog(@"%s", __func__);
    
    [super viewDidAppear:animated];
    
    
}

-(instancetype)init{
    
    self = [super init];
    
    NSLog(@"%s", __func__);
    
    return self;
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
    
    NSLog(@"%s", __func__);
    
    return self;
    
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    
    NSLog(@"%s", __func__);
    
    return self;
    
}

-(void)awakeFromNib{
    
    NSLog(@"%s", __func__);
    
    [super awakeFromNib];
}

@end

结论总结

  1. 如果控制器是直接通过代码创建出来的,并且调用方法为[[CustomViewController alloc]init]创建VC的生命周期为:
-[CustomViewController initWithNibName:bundle:]
-[CustomViewController init]
-[CustomViewController loadView]
-[CustomViewController viewDidLoad]
-[CustomViewController viewDidAppear:]

  1. 如果创建控制器时勾选了XIB,那么调用方法为[[CustomViewController alloc]init]创建VC时的生命周期为:
-[CustomViewController initWithNibName:bundle:] 
-[CustomViewController init]
-[CustomViewController loadView]
-[CustomViewController viewDidLoad]
-[CustomViewController viewDidAppear:]

  1. 如果是通过Nib创建控制器,那么VC的生命周期为:
-[ViewController initWithCoder:]
-[ViewController awakeFromNib]
-[ViewController loadView]
-[ViewController viewDidLoad]
-[ViewController viewDidAppear:]

你可能感兴趣的:(UIView与UIViewController的生命周期)