码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发UI篇—懒加载

时间:2016-08-04 13:32:21      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

1.懒加载基本

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

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

2.使用懒加载的好处:

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

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

3.代码示例

  1 //
  2 //  SLViewController.m
  3 //  03-图片浏览器初步
  4 //
  5 //  Created by apple on 14-5-21.
  6 //  Copyright (c) 2014年 itcase. All rights reserved.
  7 // 
  8 
  9 #import "SLViewController.h"
 10 
 11 #define PHOTOIMGW    200
 12 #define PHOTOIMGH    300
 13 #define PHOTOIMGX    60
 14 #define  PHOTOIMGY    50
 15 
 16 @interface SLViewController ()
 17 
 18 // 变量声明
 19 @property(nonatomic, strong) UILabel *firstLab;
 20 @property(nonatomic, strong) UILabel *lastLab;
 21 @property(nonatomic, strong) UIImageView *icon;
 22 @property(nonatomic, strong) UIButton *leftBtn;
 23 @property(nonatomic, strong) UIButton *rightBtn;
 24 @property(nonatomic, strong) NSArray *array;
 25 @property(nonatomic, assign) int i;
 26 
 27 -(void)change;
 28 @end
 29 
 30 @implementation SLViewController
 31 
 32 - (void)viewDidLoad
 33 {
 34     [super viewDidLoad];
 35     [self change];
 36 }
 37 
 38 -(void)change
 39 {
 40     //先get再set
 41     [self.firstLab setText:[NSString stringWithFormat:@"%d / 5",self.i + 1]];
 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(PHOTOIMGX, PHOTOIMGY, PHOTOIMGW, PHOTOIMGH)];
 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(PHOTOIMGX+PHOTOIMGW+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开发UI篇—懒加载

标签:

原文地址:http://www.cnblogs.com/zengshuilin/p/5736179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!