zoukankan      html  css  js  c++  java
  • iOS基础之懒加载

      什么是懒加载,即延迟加载,在你需要的时候才加载,也就是说在不需要的时候是不会加载的,减小了占用内存。当然在使用懒加载的时候要注意先加一个判断去判断有无。

      为什么要使用懒加载呢?除了上述说的能够减小占用内存,还有就是不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强,每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合。

      代码演示:

      

    #import "RootViewController.h"
    
    @interface RootViewController ()
    @property(nonatomic,retain)UILabel *label;
    @property (nonatomic,retain)UITextField *textField;
    @end
    
    @implementation RootViewController
    //懒加载
    - (UILabel *)label{
        if (_label == nil) {
            _label = [[UILabel alloc]initWithFrame:CGRectMake(200, 100, 100, 20)];
            
        }return _label;
    }
    - (UITextField *)textField{
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        return _textField;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
  • 相关阅读:
    浅谈localStorage和sessionStorage的相关用法
    v-for中:key的作用总结
    textarea的placeholder无效问题解决
    6月10日
    6月9日
    6月8日
    6月7日
    6月6日
    10月5日
    6月4日
  • 原文地址:https://www.cnblogs.com/16-jkd/p/5205719.html
Copyright © 2011-2022 走看看