IOS懒加载

时间:2021-07-07 13:38:44

1.懒加载基本

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

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

2.使用懒加载的好处:

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

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

3.代码示例

IOS懒加载
  1 //
2 // YYViewController.m
3 // 03-图片浏览器初步
4 //
5 // Created by apple on 14-5-21.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 #define POTOIMGW 200
12 #define POTOIMGH 300
13 #define POTOIMGX 60
14 #define POTOIMGY 50
15
16 @interface YYViewController ()
17
18 @property(nonatomic,strong)UILabel *firstlab;
19 @property(nonatomic,strong)UILabel *lastlab;
20 @property(nonatomic,strong)UIImageView *icon;
21 @property(nonatomic,strong)UIButton *leftbtn;
22 @property(nonatomic,strong)UIButton *rightbtn;
23 @property(nonatomic,strong)NSArray *array;
24 @property(nonatomic ,assign)int i;
25 -(void)change;
26 @end
27
28
29
30 @implementation YYViewController
31
32 - (void)viewDidLoad
33 {
34 [super viewDidLoad];
35 [self change];
36 }
37
38 -(void)change
39 {
40 [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
41 //先get再set
42
43 self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]];
44 self.lastlab.text=self.array[self.i][@"desc"];
45
46 self.leftbtn.enabled=(self.i!=0);
47 self.rightbtn.enabled=(self.i!=4);
48 }
49
50 //延迟加载
51 /**1.图片的序号标签*/
52 -(UILabel *)firstlab
53 {
54 //判断是否已经有了,若没有,则进行实例化
55 if (!_firstlab) {
56 _firstlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
57 [_firstlab setTextAlignment:NSTextAlignmentCenter];
58 [self.view addSubview:_firstlab];
59 }
60 return _firstlab;
61 }
62
63 /**2.图片控件的延迟加载*/
64 -(UIImageView *)icon
65 {
66 //判断是否已经有了,若没有,则进行实例化
67 if (!_icon) {
68 _icon=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
69 UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
70 _icon.image=image;
71 [self.view addSubview:_icon];
72 }
73 return _icon;
74 }
75
76 /**3.描述控件的延迟加载*/
77 -(UILabel *)lastlab
78 {
79 //判断是否已经有了,若没有,则进行实例化
80 if (!_lastlab) {
81 _lastlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
82 [_lastlab setTextAlignment:NSTextAlignmentCenter];
83 [self.view addSubview:_lastlab];
84 }
85 return _lastlab;
86 }
87
88 /**4.左键按钮的延迟加载*/
89 -(UIButton *)leftbtn
90 {
91 //判断是否已经有了,若没有,则进行实例化
92 if (!_leftbtn) {
93 _leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
94 _leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
95 [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
96 [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
97 [self.view addSubview:_leftbtn];
98 [_leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
99 }
100 return _leftbtn;
101
102 }
103
104 /**5.右键按钮的延迟加载*/
105 -(UIButton *)rightbtn
106 {
107 if (!_rightbtn) {
108 _rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
109 _rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
110 [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
111 [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
112 [self.view addSubview:_rightbtn];
113 [_rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
114 }
115 return _rightbtn;
116 }
117
118 //array的get方法
119 -(NSArray *)array
120 {
121 if (_array==nil) {
122 NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
123 _array=[[NSArray alloc]initWithContentsOfFile:path];
124 }
125 return _array;
126 }
127
128 -(void)rightclick:(UIButton *)btn
129 {
130 self.i++;
131 [self change];
132 }
133
134 -(void)leftclick:(UIButton *)btn
135 {
136 self.i--;
137 [self change];
138 }
139
140 @end
IOS懒加载