iOS開發UI篇—懶加載

iOS開發UI篇—懶加載瀏覽器

1.懶加載基本app

懶加載——也稱爲延遲加載,即在須要的時候才加載(效率低,佔用內存小)。所謂懶加載,寫的是其get方法.ide

注意:若是是懶加載的話則必定要注意先判斷是否已經有了,若是沒有那麼再去進行實例化atom

2.使用懶加載的好處:spa

(1)沒必要將建立對象的代碼所有寫在viewDidLoad方法中,代碼的可讀性更強code

(2)每一個控件的getter方法中分別負責各自的實例化處理,代碼彼此之間的獨立性強,鬆耦合orm

3.代碼示例對象

複製代碼

//
// YYViewController.m
// 03-圖片瀏覽器初步
//
// Created by apple on 14-5-21.
// Copyright (c) 2014年 itcase. All rights reserved.
//blog

#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

 
 
複製代碼
相關文章
相關標籤/搜索