viewDidLoad与initWithNibName的调用时机

Posted yinyanlyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了viewDidLoad与initWithNibName的调用时机相关的知识,希望对你有一定的参考价值。

加载一个ViewController,我们通常使用init或者initWithNibName方法初始化,但是有的时候会出现一些需要初始化的值没有得到正确的设置,下面给出代码看下实际的效果。

第一种情况:正常初始化

FatherViewController:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        NSLog(@"F_init start ");
        self.FPContent = @"F_我是父类初始化的";
        NSLog(@"F_init end");
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"F_View");
    NSLog(@"父类的属性值:%@",self.FPContent);
}

SubViewController:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        NSLog(@"S_init start");
        self.FPContent = @"S_我是子类初始化的";
        NSLog(@"S_init end");
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"S_View");
    NSLog(@"属性值:%@",self.FPContent);
}

执行结果输出如下:

2018-02-06 16:13:07.236401+0800 YYDemo[70368:2809851] F_init start
2018-02-06 16:13:07.236593+0800 YYDemo[70368:2809851] F_init end
2018-02-06 16:13:07.236878+0800 YYDemo[70368:2809851] S_init start
2018-02-06 16:13:07.236967+0800 YYDemo[70368:2809851] S_init end
2018-02-06 16:13:07.411284+0800 YYDemo[70368:2809851] F_View
2018-02-06 16:13:07.411469+0800 YYDemo[70368:2809851] 父类的属性值:S_我是子类初始化的
2018-02-06 16:13:07.411604+0800 YYDemo[70368:2809851] S_View
2018-02-06 16:13:07.411700+0800 YYDemo[70368:2809851] 属性值:S_我是子类初始化的

 

第二种情况:在initWithNibName中调用了viewDidLoad

FatherViewController:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        NSLog(@"F_init start ");
        self.FPContent = @"F_我是父类初始化的";
        NSLog(@"F_init end");
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"F_View");
    NSLog(@"父类的属性值:%@",self.FPContent);
}

SubViewController:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        NSLog(@"S_init start");
        UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(88, 20, 100, 20)];
        la.text = self.FPContent;
        [self.view addSubview:la];
        self.FPContent = @"S_我是子类初始化的";
        NSLog(@"S_init end");
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"S_View");
    NSLog(@"属性值:%@",self.FPContent);
}    

执行结果输出如下:

2018-02-06 16:19:37.594282+0800 YYDemo[71147:2825509] F_init start
2018-02-06 16:19:37.594492+0800 YYDemo[71147:2825509] F_init end
2018-02-06 16:19:37.594592+0800 YYDemo[71147:2825509] S_init start
2018-02-06 16:19:37.718493+0800 YYDemo[71147:2825509] F_View
2018-02-06 16:19:37.718672+0800 YYDemo[71147:2825509] 父类的属性值:F_我是父类初始化的
2018-02-06 16:19:37.718773+0800 YYDemo[71147:2825509] S_View
2018-02-06 16:19:37.718869+0800 YYDemo[71147:2825509] 属性值:F_我是父类初始化的
2018-02-06 16:19:37.719246+0800 YYDemo[71147:2825509] S_init end

 

由以上执行结果可知:

在初始化方法总调用View属性的时候就会调用viewDidLoad方法。

viewDidLoad方法一般在视图树构建完成以后调用一次,但是如果VC的View属性被引用,View为nil 的时候也会调用viewDidLoad方法,提前调动该方法可能就会导致某些值不能得到正确的初始化。

 

以上是关于viewDidLoad与initWithNibName的调用时机的主要内容,如果未能解决你的问题,请参考以下文章

UIViewController viewDidLoad vs. viewWillAppear:什么是适当的分工?

ViewDidLoad 没有被调用

再次调用 ViewDidLoad 方法

iOS 6 中的 viewDidLoad 调用一次?

React Native - 故事板作为启动屏幕不调用 viewDidLoad

从 Objc 超类继承的 Swift 子类中未调用 viewDidLoad