iOS開發UI篇—懶載入

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—懶載入

1.懶載入基本

懶載入——也稱為消極式載入,即在需要的時候才載入(效率低,佔用記憶體小)。所謂懶載入,寫的是其get方法.

注意:如果是懶載入的話則一定要注意先判斷是否已經有了,如果沒有那麼再去進行執行個體化

2.使用懶載入的好處:

(1)不必將建立對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強

(2)每個控制項的getter方法中分別負責各自的執行個體化處理,代碼彼此之間的獨立性強,松耦合

3.程式碼範例

  1 //  2 //  YYViewController.m  3 //  03-圖片瀏覽器初步  4 //  5 //  Created by apple on 14-5-21.  6 //  Copyright (c) 2014年 itcase. All rights reserved.  7 //  8   9 #import "YYViewController.h" 10  11 #define POTOIMGW    200 12 #define POTOIMGH    300 13 #define POTOIMGX    60 14 #define  POTOIMGY    50 15  16 @interface YYViewController () 17  18 @property(nonatomic,strong)UILabel *firstlab; 19 @property(nonatomic,strong)UILabel *lastlab; 20 @property(nonatomic,strong)UIImageView *icon; 21 @property(nonatomic,strong)UIButton *leftbtn; 22 @property(nonatomic,strong)UIButton *rightbtn; 23 @property(nonatomic,strong)NSArray *array; 24 @property(nonatomic ,assign)int i; 25 -(void)change; 26 @end 27  28  29  30 @implementation YYViewController 31  32 - (void)viewDidLoad 33 { 34     [super viewDidLoad]; 35     [self change]; 36 } 37  38 -(void)change 39 { 40     [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]]; 41     //先get再set 42      43     self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]]; 44     self.lastlab.text=self.array[self.i][@"desc"]; 45    46     self.leftbtn.enabled=(self.i!=0); 47     self.rightbtn.enabled=(self.i!=4); 48 } 49  50 //消極式載入 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 } 75  76 /**3.描述控制項的消極式載入*/ 77 -(UILabel *)lastlab 78 { 79      //判斷是否已經有了,若沒有,則進行執行個體化 80     if (!_lastlab) { 81         _lastlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)]; 82         [_lastlab setTextAlignment:NSTextAlignmentCenter]; 83         [self.view addSubview:_lastlab]; 84     } 85     return _lastlab; 86 } 87  88 /**4.左鍵按鈕的消極式載入*/ 89 -(UIButton *)leftbtn 90 { 91      //判斷是否已經有了,若沒有,則進行執行個體化 92     if (!_leftbtn) { 93         _leftbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 94         _leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40); 95         [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; 96         [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; 97         [self.view addSubview:_leftbtn]; 98         [_leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside]; 99     }100     return _leftbtn;101 102 }103 104 /**5.右鍵按鈕的消極式載入*/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 }117 118 //array的get方法119 -(NSArray *)array120 {121     if (_array==nil) {122         NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];123         _array=[[NSArray alloc]initWithContentsOfFile:path];124     }125     return _array;126 }127 128 -(void)rightclick:(UIButton *)btn129 {130     self.i++;131     [self change];132 }133 134 -(void)leftclick:(UIButton *)btn135 {136     self.i--;137     [self change];138 }139 140 @end

iOS開發UI篇—懶載入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.