ios开发之UI基础--懒加载

1.懒加载基本

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

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

2.使用懒加载的好处:

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

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

3.代码示例








1#import "ViewController.h"

  1. #import "ViewController.h"
  2. @interface ViewController ()
  3. @property (weak, nonatomic) IBOutlet UILabel *indexLabel;
  4. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  5. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;

  6. - (IBAction)leftButton:(UIButton *)sender;
  7. - (IBAction)rightButton:(UIButton *)sender;
  8. @property (weak, nonatomic) IBOutlet UIButton *leftButton;
  9. @property (weak, nonatomic) IBOutlet UIButton *rightButton;

  10. // 定义数组, 读取plist的数据
  11. @property (nonatomic, strong) NSArray *dataArray;

  12. // 全局变量的索引
  13. @property (nonatomic, assign) NSInteger index;


  14. @end

  15. @implementation ViewController


  16. // dataArray的get方法
  17. // 懒加载, 延迟加载
  18. - (NSArray *)dataArray {

  19. if (nil == _dataArray) {
  20. // 填充数据

  21. // 1. 拿到文件路径
  22. NSString *path = [[NSBundle mainBundle] pathForResource:@"images.plist" ofType:nil];

  23. // 读取文件
  24. _dataArray = [NSArray arrayWithContentsOfFile:path];

  25. }

  26. return _dataArray;
  27. }

  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view, typically from a nib.

  31. // 初始化index
  32. _index = 1;


  33. // 更新界面
  34. [self updateUI];

  35. // // 1. 顶部的索引Label
  36. // self.indexLabel.text = [NSString stringWithFormat:@"%ld/%ld",_index,self.dataArray.count];
  37. // 
  38. // // 取出当前_index对应的字典
  39. // NSDictionary *dict = self.dataArray[_index - 1];
  40. // 
  41. // // 2. imageView的image
  42. // self.imageView.image = [UIImage imageNamed:dict[@"icon"]];
  43. // 
  44. // // 3. 下部的Label
  45. // 
  46. // self.titleLabel.text = dict[@"title"];

  47. // 4. 让上一张图片按钮不可被点击
  48. // self.leftButton.enabled = NO;

  49. }


  50. // 左侧上一张图片按钮
  51. - (IBAction)leftButton:(UIButton *)sender {

  52. // index - 1
  53. _index--;

  54. // self.rightButton.enabled = YES;
  55. // 
  56. // sender.enabled = _index != 1;

  57. // 调用更新界面的方法
  58. [self updateUI];

  59. // // 取出数组, 设置数据
  60. // NSDictionary *dict = self.dataArray[_index - 1];
  61. // 
  62. // self.indexLabel.text = [NSString stringWithFormat:@"%ld/%ld",_index,self.dataArray.count];
  63. // 
  64. // self.imageView.image = [UIImage imageNamed:dict[@"icon"]];
  65. // 
  66. // self.titleLabel.text = dict[@"title"];
  67. }


  68. // 右侧下一张图片按钮
  69. - (IBAction)rightButton:(UIButton *)sender {
  70. // index + 1
  71. _index++;

  72. // 调用更新界面的方法
  73. [self updateUI];

  74. // 设置下一张图片的按钮 , 不可被点击(只要不是最后一张图片都可以被点击)

  75. // sender.enabled = _index != self.dataArray.count;

  76. // if (_index == self.dataArray.count) {
  77. // sender.enabled = NO;
  78. // }
  79. // 
  80. // 
  81. // // 把左侧, 上一张图片的按钮置为可点击状态
  82. // self.leftButton.enabled = YES;
  83. }


  84. // 更新界面数据展示
  85. - (void)updateUI {
  86. // 取出对应的字典
  87. NSDictionary *dict = self.dataArray[_index -1];

  88. // 设置imageView的image 和 Label 的text

  89. self.imageView.image = [UIImage imageNamed:dict[@"icon"]];

  90. self.titleLabel.text = dict[@"title"];

  91. // 顶部的indexLabel
  92. self.indexLabel.text = [NSString stringWithFormat:@"%ld/%ld",_index, self.dataArray.count];

  93. self.leftButton.enabled = (_index != 1);
  94. self.rightButton.enabled = (_index != self.dataArray.count);

  95. }
  96. @end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值