UI篇—懒加载
2015-09-21 01:39
585 查看
1.懒加载基本
懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.
注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化
2.使用懒加载的好处:
(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强
(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合
3.手码懒加载创建控件的步骤
1> 定义控件属性,注意:属性必须是strong的,如下:
@property (nonatomic, strong) UIImageView *icon;
2> 在属性的getter方法中实现懒加载。
4.代码示例
懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.
注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化
2.使用懒加载的好处:
(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强
(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合
3.手码懒加载创建控件的步骤
1> 定义控件属性,注意:属性必须是strong的,如下:
@property (nonatomic, strong) UIImageView *icon;
2> 在属性的getter方法中实现懒加载。
4.代码示例
// // YYViewController.m // 03-图片浏览器初步 // // Created by apple on 14-5-21. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #define POTOIMGW 200 #define POTOIMGH 300 #define POTOIMGX 60 #define POTOIMGY 50 @interface YYViewController () @property(nonatomic,strong)UILabel *firstlab; @property(nonatomic,strong)UILabel *lastlab; @property(nonatomic,strong)UIImageView *icon; @property(nonatomic,strong)UIButton *leftbtn; @property(nonatomic,strong)UIButton *rightbtn; @property(nonatomic,strong)NSArray *array; @property(nonatomic ,assign)int i; -(void)change; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; [self change]; } -(void)change { [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]]; //先get再set self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]]; self.lastlab.text=self.array[self.i][@"desc"]; self.leftbtn.enabled=(self.i!=0); self.rightbtn.enabled=(self.i!=4); } //延迟加载 /**1.图片的序号标签*/ -(UILabel *)firstlab { //判断是否已经有了,若没有,则进行实例化 if (!_firstlab) { _firstlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)]; [_firstlab setTextAlignment:NSTextAlignmentCenter]; [self.view addSubview:_firstlab]; } return _firstlab; } /**2.图片控件的延迟加载*/ -(UIImageView *)icon { //判断是否已经有了,若没有,则进行实例化 if (!_icon) { _icon=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)]; UIImage *image=[UIImage imageNamed:@"biaoqingdi"]; _icon.image=image; [self.view addSubview:_icon]; } return _icon; } /**3.描述控件的延迟加载*/ -(UILabel *)lastlab { //判断是否已经有了,若没有,则进行实例化 if (!_lastlab) { _lastlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)]; [_lastlab setTextAlignment:NSTextAlignmentCenter]; [self.view addSubview:_lastlab]; } return _lastlab; } /**4.左键按钮的延迟加载*/ -(UIButton *)leftbtn { //判断是否已经有了,若没有,则进行实例化 if (!_leftbtn) { _leftbtn=[UIButton buttonWithType:UIButtonTypeCustom]; _leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40); [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:_leftbtn]; [_leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside]; } return _leftbtn; } /**5.右键按钮的延迟加载*/ -(UIButton *)rightbtn { if (!_rightbtn) { _rightbtn=[UIButton buttonWithType:UIButtonTypeCustom]; _rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40); [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:_rightbtn]; [_rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside]; } return _rightbtn; } //array的get方法 -(NSArray *)array { if (_array==nil) { NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; _array=[[NSArray alloc]initWithContentsOfFile:path]; } return _array; } -(void)rightclick:(UIButton *)btn { self.i++; [self change]; } -(void)leftclick:(UIButton *)btn { self.i--; [self change]; } @end
相关文章推荐
- PHP中include和require的区别详解
- java中将1转换为True的简单方法
- 在UIScrollView中使用Autolayout布局(2)
- IOS UIBezierPath使用方法详解
- RequireJS学习笔记(转)
- ARC环境下,自动释放UIViewController...
- 【WPF学习】WPF、WinForm(C#)多线程编程并更新界面(UI)/子线程更新主界面方式
- ios学习笔记——UIScrollView
- Android UI界面shape的用法
- 扩展easyui 的表单验证
- NGUI 源码分析- AnchorPoint
- hdoj2670Girl Love Value【01背包】
- 八位 UUID
- uinty3d导入错误问题解决
- iOS 【UIKit-按钮移动操作】
- Longest Consecutive Sequence——Leetcode
- leetcode - N-Queens
- 最小生成树 POJ 3625Building Roads解题报告
- iOS程序-UIScrollView的基本使用
- String、StringBuffer与StringBuilder之间区别