iOS中懒加载

1.懒加载基本spa

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

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

2.使用懒加载的好处:get

(1)没必要将建立对象的代码所有写在viewDidLoad方法中,代码的可读性更强it

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

- (UIButton *)button {方法

    if (!_button) {view

        _button = [UIButton buttonWithType:UIButtonTypeSystem];vi

        _button.center =self.view.center ;co

        _button.bounds = CGRectMake(0, 0, 60, 40);

        _button.backgroundColor = [UIColor redColor];

        [self.view addSubview:_button];

    }

    return _button;

}

相关文章
相关标签/搜索