源码03-02-09-控制器View懒加载

Posted laugh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码03-02-09-控制器View懒加载相关的知识,希望对你有一定的参考价值。

 

 

//
//  AppDelegate.m
//  09-控制器View懒加载
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 创建UIViewController控制器,控制器的view并没有创建
    // 控制器的view懒加载:第一次使用的时候才会去加载,并不是创建UIViewController控制器的时候去加载
    UIViewController *vc = [[ViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    
    self.window.rootViewController = vc;
    
    // 显示窗口
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

#pragma mark - 控制器view的默认颜色
- (void)vcViewBackground
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor redColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.center = CGPointMake(20, 100);
    [self.window addSubview:btn];
    
    // 默认控制器的view:几乎透明
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor clearColor];
    
    //    vc.view.alpha = 0;
    //    vc.view.backgroundColor = [UIColor yellowColor];
    self.window.rootViewController = vc;
    
    
    [self.window makeKeyAndVisible];

}
@end

 

//
//  ViewController.m
//  09-控制器View懒加载
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (void)loadView
{
    // 如果控制器是窗口的根控制器就可以不用设置尺寸
    self.view = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
}
//- (UIView *)view
//{
//    if (_view == nil) {
//        [self loadView];
//        [self viewDidLoad];
//    }
//    return _view;
//}
// 在viewDidLoad中打印控制器的尺寸不准确,通常在viewDidAppear
// 控制器的view加载完成的时候调用
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor blueColor];
    
    
    NSLog(@"%s",__func__);
}

#pragma mark - view完全显示的时候调用
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%@",NSStringFromCGRect(self.view.bounds));
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

以上是关于源码03-02-09-控制器View懒加载的主要内容,如果未能解决你的问题,请参考以下文章

jijin-01

Spring源码分析非懒加载的Bean实例化过程(下篇)

IOS开发之Bug--View是懒加载导致出误以为是UI加载的bug

iOS之深入了解控制器View的加载

fourColor01

Prism 源码解读2-View的加载和控制