首页 技术 正文
技术 2022年11月15日
0 收藏 507 点赞 2,153 浏览 1985 个字

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

一、懒加载基本内容

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

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

优点:

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

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

二、代码实例

  简化 viewDidLoad 方法如下:

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

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

 //firstLabel的getter方法
- (UILabel *)firstLabel {
//判断是否有了,若没有,则进行实例化
if (!_firstLabel) {
_firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[_firstLabel setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_firstLabel];
}
return _firstLabel;
} //lastLabel的getter方法
- (UILabel *)lastLabel {
if (!_lastLabel) {
_lastLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[_lastLabel setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_lastLabel];
}
return _lastLabel;
} //imageIcon的getter方法
- (UIImageView *)imageIcon {
if (!_imageIcon) {
_imageIcon = [[UIImageView alloc] initWithFrame:CGRectMake(POTOIMAGEX, POTOIMAGEY, POTOIMAGEWIDTH, POTOIMAGEHEIGHT)];
_imageIcon.image = [UIImage imageNamed:@"beauty0"];
[self.view addSubview:_imageIcon];
}
return _imageIcon;
} //leftButton的getter方法
- (UIButton *)leftButton {
if (!_leftButton) {
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
_leftButton.frame = CGRectMake(, self.view.center.y, , );
[_leftButton setBackgroundImage:[UIImage imageNamed:@"leftRow"] forState:UIControlStateNormal];
[self.view addSubview:_leftButton];
[_leftButton addTarget:self action:@selector(leftClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _leftButton;
} //rightButton的getter方法
- (UIButton *)rightButton {
if (!_rightButton) {
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(, self.view.center.y, , );
[_rightButton setBackgroundImage:[UIImage imageNamed:@"rightRow"] forState:UIControlStateNormal];
[self.view addSubview:_rightButton];
[_rightButton addTarget:self action:@selector(rightClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _rightButton;
}

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860