iOS開發之UIView與UIViewController的生命週期總結

iOS開發中,建立View常見的兩種方式一個是純代碼,一個是藉助於XIB;建立ViewController常見的也有兩種方式一個是純代碼,一個是藉助於StoryBoard。 經過交流我發現不少童鞋對這些概念都很模糊,因而經過實驗寫一篇博客供參考。bash

重寫View的以下方法

@implementation YFView

-(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
複製代碼

重寫ViewController的以下方法

@implementation YFViewController

-(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. 若是控制器是直接經過代碼建立出來的,而且調用方法爲[[YFViewController alloc]init]建立VC的生命週期爲:
-[YFViewController initWithNibName:bundle:]
-[YFViewController init]
-[YFViewController loadView]
-[YFViewController viewDidLoad]
-[YFViewController viewDidAppear:]
複製代碼
  1. 若是建立控制器時勾選了XIB,那麼調用方法爲[[YFViewController alloc]init]建立VC時的生命週期爲:
-[YFViewController initWithNibName:bundle:] 
-[YFViewController init]
-[YFViewController loadView]
-[YFViewController viewDidLoad]
-[YFViewController viewDidAppear:]
複製代碼
  1. 若是是經過SB建立控制器,那麼VC的生命週期爲:
-[ViewController initWithCoder:]
-[ViewController awakeFromNib]
-[ViewController loadView]
-[ViewController viewDidLoad]
-[ViewController viewDidAppear:]
複製代碼
  1. 若是純代碼建立UIView,調用方式爲YFView *yfView = [[YFView alloc]init];那麼它的生命週期爲:
-[YFView initWithFrame:]
-[YFView init]
-[YFView layoutSubviews]
複製代碼
  1. 若是純代碼建立UIView,調用方式爲YFView *yfView = [[YFView alloc]initWithFrame:[UIScreen mainScreen].bounds];那麼它的生命週期爲:
-[YFView initWithFrame:]
-[YFView layoutSubviews]
複製代碼
  1. 若是經過XIB建立UIView,調用方式爲NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"YFView" owner:nil options:nil];[array lastObject];那麼它的生命週期爲:
-[YFView initWithCoder:]
-[YFView awakeFromNib]
-[YFView layoutSubviews]
複製代碼
相關文章
相關標籤/搜索