iOS开发 懒加载
懒加载,顾名思义,在iOS程序中,当需要某些资源时才加载该资源,而不是在初始化时一次性加载所有的资源。下面以一个小例子说明懒加载的用法。

如果不使用懒加载,上图中的五个控件,需要在ViewController的- (void)viewDidLoad方法中创建,代码如下。
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 实例化控件 6 // 1. 序号标签 7 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 40)]; 8 label.text = @"1/5"; 9 label.textAlignment = NSTextAlignmentCenter; 10 [self.view addSubview:label]; 11 12 self.noLabel = label; 13 14 // 2. 图片视图 15 CGFloat imageW = 200; 16 CGFloat imageX = (320 - imageW) / 2; 17 CGFloat imageH = 200; 18 CGFloat imageY = 80; 19 // 实例化一个图像视图 20 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)]; 21 22 // 实例化一个图像 23 UIImage *image = [UIImage imageNamed:@"biaoqingdi"]; 24 // 将图像显示在imageView中 25 imageView.image = image; 26 27 [self.view addSubview:imageView]; 28 29 self.icon = imageView; 30 31 // 3. 描述标签 32 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 80)]; 33 label1.text = @"神马表情都弱爆了"; 34 label1.textAlignment = NSTextAlignmentCenter; 35 [self.view addSubview:label1]; 36 37 self.descLabel = label1; 38 39 // 4. 左边的按钮 40 UIButton *leftBtn = [[UIButton alloc] init]; 41 // 设置按钮的背景图像 42 [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; 43 [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; 44 45 leftBtn.frame = CGRectMake(0, 0, 40, 40); 46 leftBtn.center = CGPointMake(self.icon.frame.origin.x / 2, self.icon.center.y); 47 48 [self.view addSubview:leftBtn]; 49 50 [leftBtn addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside]; 51 52 self.leftButton = leftBtn; 53 54 // 5. 右边的按钮 55 UIButton *rightBtn = [[UIButton alloc] init]; 56 [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; 57 [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; 58 59 rightBtn.frame = CGRectMake(0, 0, 40, 40); 60 rightBtn.center = CGPointMake(self.view.frame.size.width - self.icon.frame.origin.x / 2, self.icon.center.y); 61 62 [self.view addSubview:rightBtn]; 63 64 [rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside]; 65 66 self.rightButton = rightBtn; 67 68 69 }
如果将每个控件的创建代码封装成一个方法,代码的可读性更强。懒加载就是将控件的创建代码放到getter方法中,当需要该控件时,如果该控件已经存在,直接返回该控件;如果该控件不存在,则创建控件。代码如下。
1 /** 序号标签 */ 2 - (UILabel *)noLabel 3 { 4 if (!_noLabel) { 5 _noLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 40)]; 6 _noLabel.textAlignment = NSTextAlignmentCenter; 7 8 [self.view addSubview:_noLabel]; 9 } 10 return _noLabel; 11 } 12 13 - (UIImageView *)icon 14 { 15 if (!_icon) { 16 CGFloat imageW = 200; 17 CGFloat imageX = (320 - imageW) / 2; 18 CGFloat imageH = 200; 19 CGFloat imageY = 80; 20 21 _icon = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)]; 22 23 [self.view addSubview:_icon]; 24 } 25 return _icon; 26 } 27 28 - (UILabel *)descLabel 29 { 30 if (!_descLabel) { 31 _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 180)]; 32 _descLabel.numberOfLines = 0; 33 _descLabel.textAlignment = NSTextAlignmentCenter; 34 35 [self.view addSubview:_descLabel]; 36 } 37 return _descLabel; 38 } 39 40 - (UIButton *)leftButton 41 { 42 if (!_leftButton) { 43 _leftButton = [[UIButton alloc] init]; 44 // 设置按钮的背景图像 45 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; 46 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; 47 48 _leftButton.frame = CGRectMake(0, 0, 40, 40); 49 _leftButton.center = CGPointMake(self.icon.frame.origin.x / 2, self.icon.center.y); 50 // _leftButton.center = CGPointMake(self.icon.frame.origin.x / 2, _icon.center.y); 51 52 [self.view addSubview:_leftButton]; 53 54 [_leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside]; 55 } 56 return _leftButton; 57 } 58 59 - (UIButton *)rightButton 60 { 61 if (!_rightButton) { 62 _rightButton = [[UIButton alloc] init]; 63 [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; 64 [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; 65 66 _rightButton.frame = CGRectMake(0, 0, 40, 40); 67 _rightButton.center = CGPointMake(self.view.frame.size.width - self.icon.frame.origin.x / 2, self.icon.center.y); 68 69 [self.view addSubview:_rightButton]; 70 71 [_rightButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside]; 72 } 73 return _rightButton; 74 }
使用懒加载,每个控件的getter方法负责自己的实例化,代码之间的独立性强,弱耦合。
浙公网安备 33010602011771号