zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Java连接MySQL数据库——含步骤和代码
    JAVA join()方法
    Canny边缘检测
    JAVA NIO——Buffer和FileChannel
    剑指offer——树的子结构 (JAVA代码)
    校测链接集合贴
    莫比乌斯反演
    可持久化数据结构
    thusc2021题解
    THUSC2021夏令营自闭记
  • 原文地址:https://www.cnblogs.com/wjq-Law/p/5118444.html
Copyright © 2011-2022 走看看