zoukankan      html  css  js  c++  java
  • View的懒加载

    AppDelegate.m

    #import "AppDelegate.h"

    #import "ViewController.h"

    @interface AppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

        ViewController *vc = [[ViewController alloc] init];

    //    vc.view.backgroundColor = [UIColor purpleColor];//view的懒加载 View 的懒加载只有需要的时候才加载

    //    NSLog(@"aaaaaa");

        window.rootViewController = vc;

        NSLog(@"aaaaaaa");

       

        //显示控制器view

        [window makeKeyAndVisible];

        NSLog(@"bbbbbb");

        self.window = window;

        return YES;

    }


    ViewController.m

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSLog(@"%s",__func__);

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

    另一种情况

    #import "AppDelegate.h"

    #import "ViewController.h"

    @interface AppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

        ViewController *vc = [[ViewController alloc] init];

        vc.view.backgroundColor = [UIColor purpleColor];//view的懒加载 View 的懒加载只有需要的时候才加载

        NSLog(@"aaaaaa");

        window.rootViewController = vc;

    //    NSLog(@"aaaaaaa");

       

        //显示控制器view

        [window makeKeyAndVisible];

        NSLog(@"bbbbbb");

        self.window = window;

        return YES;

    }

     

    1.懒加载基本

    懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.

    注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化

    2.使用懒加载的好处:

    (1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

    (2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

  • 相关阅读:
    jchdl
    jchdl
    UVa 10256 (判断两个凸包相离) The Great Divide
    UVa 11168 (凸包+点到直线距离) Airport
    LA 2572 (求可见圆盘的数量) Kanazawa
    UVa 10652 (简单凸包) Board Wrapping
    UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
    UVa 10674 (求两圆公切线) Tangents
    UVa 11796 Dog Distance
    LA 3263 (平面图的欧拉定理) That Nice Euler Circuit
  • 原文地址:https://www.cnblogs.com/Lu2015-10-03/p/5147832.html
Copyright © 2011-2022 走看看