iOS开发基础-图片切换(4)之懒加载

延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善。

 

一、懒加载基本内容

  懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法。

  注意:懒加载时一定要先判断该属性是否为 nil ,如果为 nil 才进行实例化。

优点:

  1) viewDidLoad 中创建对象的方法用懒加载创建,增加可读性。

  2)每个控件的 getter 方法中负责各自的实例化处理,增加代码之间的独立性。

二、代码实例

  简化 viewDidLoad 方法如下:

1 - (void)viewDidLoad {
2     [super viewDidLoad];
3     [self change];   //初始化界面
4 }

  对2个 UILabel ,2个 UIButton 和1个 UIImageView 的 getter 方法进行修改:

 1 //firstLabel的getter方法
 2 - (UILabel *)firstLabel {
 3     //判断是否有了,若没有,则进行实例化
 4     if (!_firstLabel) {
 5         _firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 300, 30)];
 6         [_firstLabel setTextAlignment:NSTextAlignmentCenter];
 7         [self.view addSubview:_firstLabel];
 8     }
 9     return _firstLabel;
10 }
11 
12 //lastLabel的getter方法
13 - (UILabel *)lastLabel {
14     if (!_lastLabel) {
15         _lastLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 400, 300, 30)];
16         [_lastLabel setTextAlignment:NSTextAlignmentCenter];
17         [self.view addSubview:_lastLabel];
18     }
19     return _lastLabel;
20 }
21 
22 //imageIcon的getter方法
23 - (UIImageView *)imageIcon {
24     if (!_imageIcon) {
25         _imageIcon = [[UIImageView alloc] initWithFrame:CGRectMake(POTOIMAGEX, POTOIMAGEY, POTOIMAGEWIDTH, POTOIMAGEHEIGHT)];
26         _imageIcon.image = [UIImage imageNamed:@"beauty0"];
27         [self.view addSubview:_imageIcon];
28     }
29     return _imageIcon;
30 }
31 
32 //leftButton的getter方法
33 - (UIButton *)leftButton {
34     if (!_leftButton) {
35         _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
36         _leftButton.frame = CGRectMake(10, self.view.center.y, 30, 53);
37         [_leftButton setBackgroundImage:[UIImage imageNamed:@"leftRow"] forState:UIControlStateNormal];
38         [self.view addSubview:_leftButton];
39         [_leftButton addTarget:self action:@selector(leftClicked:) forControlEvents:UIControlEventTouchUpInside];
40     }
41     return _leftButton;
42 }
43 
44 //rightButton的getter方法
45 - (UIButton *)rightButton {
46     if (!_rightButton) {
47         _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
48         _rightButton.frame = CGRectMake(280, self.view.center.y, 30, 53);
49         [_rightButton setBackgroundImage:[UIImage imageNamed:@"rightRow"] forState:UIControlStateNormal];
50         [self.view addSubview:_rightButton];
51         [_rightButton addTarget:self action:@selector(rightClicked:) forControlEvents:UIControlEventTouchUpInside];
52     }
53     return _rightButton;
54 }

 

参考博客:iOS开发UI篇—懒加载

实例代码:http://pan.baidu.com/s/1dElhMmP

转载于:https://www.cnblogs.com/wjq-Law/p/5118444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值