iOS开发UI篇—懒加载
来源:互联网 发布:macair windows 编辑:程序博客网 时间:2024/05/24 01:02
1.懒加载基本
懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.
注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化
2.使用懒加载的好处:
(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强
(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合
3.代码示例
//延迟加载 51 /**1.描述控件标签*/ 52 -(UILabel *)firstlab 53 { 54 //判断是否已经有了,若没有,则进行实例化 55 if (!_firstlab) { 56 _firstlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)]; 57 [_firstlab setTextAlignment:NSTextAlignmentCenter]; 58 [self.view addSubview:_firstlab]; 59 } 60 return _firstlab; 61 } 62 63 /**2.图片控件的延迟加载*/ 64 -(UIImageView *)icon 65 { 66 //判断是否已经有了,若没有,则进行实例化 67 if (!_icon) { 68 _icon=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)]; 69 UIImage *image=[UIImage imageNamed:@"biaoqingdi"]; 70 _icon.image=image; 71 [self.view addSubview:_icon]; 72 } 73 return _icon; 74 }
/**3.右键按钮的延迟加载*/105 -(UIButton *)rightbtn106 {107 if (!_rightbtn) {108 _rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];109 _rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);110 [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];111 [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];112 [self.view addSubview:_rightbtn];113 [_rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];114 }115 return _rightbtn;116 }
0 0
- iOS开发 UI篇 — 懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- IOS开发UI篇—懒加载
- iOS开发UI篇—懒加载
- 程序员必须知道的10大基础实用算法及其讲解
- MVC3的新特性介绍
- 关于UITabBarController的UITabBar隐藏问题
- Java编写一个简单的TCP通信程序。服务器发送一条字符串,客户端接收该信息并显示。
- DVSDK码率控制的理解
- iOS开发UI篇—懒加载
- C# 判断闰年
- 【C++】拷贝构造函数之浅复制与深复制
- 创业App对接三方支付辛酸史
- Android Studio导入Android源码
- Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead. 解决办法
- 网络时延——发送时延和传播时延
- android之标签icon在文本多行背后或首次位置显示
- 记录一下使用百度Ueditor副编辑器的过程及碰到的问题